Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/299.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何通过重启保持玩家皮肤_C#_Unity3d_Scene Manager - Fatal编程技术网

C# 如何通过重启保持玩家皮肤

C# 如何通过重启保持玩家皮肤,c#,unity3d,scene-manager,C#,Unity3d,Scene Manager,我正在努力保持玩家的皮肤,即使是在重新加载场景或转移到新场景时。播放器对象会改变每个场景 玩家皮肤在暂停菜单中选择,有三个按钮。这些按钮中的每一个都调用下面脚本中的函数。我试图调用其中一个函数,基于PlayerPrefs int所包含的值,函数确实会被调用;但是抛出错误MissingReferenceException:类型为“GameObject”的对象已被销毁,但您仍在尝试访问它 下面是我已经尝试过的,但这会在场景重新加载(死亡)时抛出一个错误 我不知道我做错了什么 using System

我正在努力保持玩家的皮肤,即使是在重新加载场景或转移到新场景时。播放器对象会改变每个场景

玩家皮肤在暂停菜单中选择,有三个按钮。这些按钮中的每一个都调用下面脚本中的函数。我试图调用其中一个函数,基于
PlayerPrefs int
所包含的值,函数确实会被调用;但是抛出错误
MissingReferenceException:类型为“GameObject”的对象已被销毁,但您仍在尝试访问它

下面是我已经尝试过的,但这会在场景重新加载(死亡)时抛出一个错误 我不知道我做错了什么

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement; 

public class Pausemenu : MonoBehaviour
{


    public static bool gameIsPaused = false;

    public GameObject pauseMenuUI;

    public Material BelgianMat;

    public Material Ball2;

    public Material Rainbow;

    public GameObject Player;

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Escape)) {

            if (gameIsPaused) {
                Resume();
            } else {
                Pause();
            }

        }
    }
    public void Resume(){
        pauseMenuUI.SetActive(false);
        Time.timeScale = 1f;
        gameIsPaused = false;
    }

    void Pause() {
         pauseMenuUI.SetActive(true);
         Time.timeScale = 0f;
         gameIsPaused = true;
    }
    
    public void LoadMenu() {
        Time.timeScale = 1f;
        gameIsPaused = false;
        SceneManager.LoadScene("Menu");
    }

    public void QuitGame() {
        Debug.Log("Quitting");
        Application.Quit();
    }

    public void ApplyBelgian() {
        Player.GetComponent<Renderer>().material = BelgianMat;
        PlayerPrefs.SetInt("playerMat", 0);
    }

    public void ApplyBall2() {
        Player.GetComponent<Renderer>().material = Ball2;
        Debug.Log("Applied ball two");
        PlayerPrefs.SetInt("playerMat", 1);
    }

    public void ApplyRainbow() {
        Player.GetComponent<Renderer>().material = Rainbow;
        PlayerPrefs.SetInt("playerMat", 2);
        }

     void OnEnable()
    {
        Debug.Log("OnEnable called");
        SceneManager.sceneLoaded += OnSceneLoaded;
    }

    // called second
    void OnSceneLoaded(Scene scene, LoadSceneMode mode)
    {
        Debug.Log("OnSceneLoaded: " + scene.name);
        Debug.Log(mode);
        if (PlayerPrefs.GetInt("playerMat") == 0) {
            ApplyBelgian();
        } 
        else if (PlayerPrefs.GetInt("playerMat") == 1) {
            Debug.Log("gonna apply ball 2");
            ApplyBall2();
        } 
        else if (PlayerPrefs.GetInt("playerMat") == 2) {
            ApplyRainbow();
        }
    }
}
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
使用UnityEngine.SceneManagement;
公共课暂停:单一行为
{
公共静态bool gameIsPaused=false;
公共游戏对象暂停;
公共材料比利时猫;
公共材料2;
公共材料彩虹;
公共游戏对象玩家;
//每帧调用一次更新
无效更新()
{
if(Input.GetKeyDown(KeyCode.Escape)){
如果(游戏暂停){
恢复();
}否则{
暂停();
}
}
}
公众简历(){
pauseMenuUI.SetActive(false);
Time.timeScale=1f;
gamespaused=false;
}
无效暂停(){
pauseMenuUI.SetActive(真);
Time.timeScale=0f;
gamespaused=true;
}
公共void加载菜单(){
Time.timeScale=1f;
gamespaused=false;
SceneManager.LoadScene(“菜单”);
}
公共游戏(){
Debug.Log(“退出”);
Application.Quit();
}
公共无效ApplyBelgian(){
Player.GetComponent().material=BelgianMat;
PlayerPrefs.SetInt(“playerMat”,0);
}
public void applyball 2(){
Player.GetComponent().material=Ball2;
Log(“应用球2”);
PlayerPrefs.SetInt(“playerMat”,1);
}
公共无效ApplyRainbow(){
Player.GetComponent().material=Rainbow;
PlayerPrefs.SetInt(“playerMat”,2);
}
void OnEnable()
{
Log(“被调用的OnEnable”);
SceneManager.sceneLoaded+=在sceneLoaded上;
}
//第二名
void OnSceneLoaded(场景,加载场景模式)
{
Log(“OnSceneLoaded:+scene.name”);
Log(模式);
if(PlayerPrefs.GetInt(“playerMat”)==0){
ApplyBelgian();
} 
else if(PlayerPrefs.GetInt(“playerMat”)==1){
Log(“将应用球2”);
ApplyBall2();
} 
else if(PlayerPrefs.GetInt(“playerMat”)==2){
ApplyRainbow();
}
}
}

我不知道为什么,但加载场景后,对播放器对象的引用似乎可能会被破坏。如果是这种情况,请向玩家对象添加一个“玩家”标记,并将其添加到统一函数:
Player=GameObject.FindWithTag(“玩家”)

可能是因为代理未被删除。检查一下。仅供参考
sceneLoaded
事件为未知数:您可以改用
Awake/Start
回调。