首页 > 解决方案 > “CanvasGroup”类型的对象已被销毁,但您仍在尝试访问它

问题描述

我正在为我的游戏测试一些代码:尝试使一个已经被拾取的对象在重新加载关卡时不再产生。出于某种原因,我现在收到此错误,我似乎还没有找到解决方案(看到很多人在谷歌搜索时遇到了这个问题,但似乎没有一个修复程序适合)

没有尝试太多,因为它非常个人化。一个修复对我不起作用,因为我无法找到确切的位置(由于不同的脚本名称..)

public class GameManager_GameLevel : MonoBehaviour
{
    //MANAGER
    private const int COIN_SCORE_AMOUNT = 5;
    public static GameManager_GameLevel Instance { set; get; }
    public bool isDead { set; get; }
    private bool isGameStarted = false;
    private PlayerMotor_GameLevel motor;
    public Text coinText;
    private float coinScore;
    private bool gameStarted;
    public GameObject player;
    public GameObject cameraFollow;
    private Vector3 deathPosition = new Vector3(0, 0, 30);
    public Button buttonReplay;

    //FADE
    private CanvasGroup fadeGroup;
    private float fadeInDuration = 2.0f;
    //ANIMATOR
    public Animator deathMenuAnim;
    public Animator pauseMenuAnim;
    public Animator playerAnim;


    private void Awake()
    {
        Instance = this;
        motor = GameObject.FindGameObjectWithTag("Player").GetComponent<PlayerMotor_GameLevel>();
        coinText.text = coinScore.ToString("0");
    }

    private void Start()
    {
       SceneManager.LoadScene(SaveManager.Instance.currentLevel.ToString(), LoadSceneMode.Additive);

        fadeGroup = FindObjectOfType<CanvasGroup>();
        fadeGroup.alpha = 1;
    }

    public void Update()
    {
       if(Time.timeSinceLevelLoad <= fadeInDuration)
        {
            fadeGroup.alpha = 1 - (Time.timeSinceLevelLoad / fadeInDuration);
        }
        else if(!gameStarted)
        {
            fadeGroup.alpha = 0;
            gameStarted = true;
        }

        GameStarted();
    }

    public void GameStarted()
    {
        if(MobileInput_GameLevel.Instance.Tap && !isGameStarted)
        {
            isGameStarted = true;
            motor.StartRunning();
            FindObjectOfType<DecorSpawner_GameLevel>().isScrolling = true;
            FindObjectOfType<CameraMotor_GameLevel>().isMoving = true;
        }
    }

    public void GetCoin()
    {
        coinScore ++;
        coinText.text = coinScore.ToString("0");
    }

    public void OnDeath()
    {
        isDead = true;
        FindObjectOfType<DecorSpawner_GameLevel>().isScrolling = false;
        deathMenuAnim.SetTrigger("Dead");
    }

    public void Pause()
    {
        Time.timeScale = 0f;
        pauseMenuAnim.SetBool("Pause", true);
    }

    public void ResumeGame()
    {
        Time.timeScale = 1f;
        pauseMenuAnim.SetBool("Pause", false);
    }

    public void RetryButton()
    {
        Time.timeScale = 1f;
        SceneManager.LoadScene("GameLevel");
    }
    public void QuitButton()
    {
        Time.timeScale = 1f;
        SceneManager.LoadScene("MainMenu");
    }

    public void ReplayButton()
    {
        buttonReplay.gameObject.SetActive(false);
        motor.StartRunning();
        FindObjectOfType<DecorSpawner_GameLevel>().isScrolling = true;
        FindObjectOfType<CameraMotor_GameLevel>().isMoving = true;
    }
    public void Respawn()
    {
        AdController.Instance.PlayRewardedVideo();
        buttonReplay.gameObject.SetActive(true);
        player.transform.position = player.transform.position - deathPosition;
        cameraFollow.transform.position = cameraFollow.transform.position - deathPosition;
        playerAnim.SetTrigger("Reset");
        deathMenuAnim.SetTrigger("Reset");
    }
}

这个问题出现在这个脚本上,但也出现在其他脚本上。它实际上发生在我已经开始玩游戏后单击游戏中的按钮时调用的任何脚本,例如“重新启动,菜单,选项”。一切都很好,直到实际调用第二个场景。

哦,还有,我实际上并没有破坏任何东西,以前一切都很好。该错误突然出现,我什至没有更改所有这些脚本的任何内容。

编辑:在有人指出我以前的标题令人困惑之后。对象类型可能是 CanvasGroup(第一个出现的),但我也看到了 Text 等。

标签: c#unity3d

解决方案


将此应用到要在场景之间保留的游戏对象。

参考:

var gameObject = //whatever
DontDestroyOnLoad(gameObject);

如:

    private void Awake()
    {         
        DontDestroyOnLoad(fadeGroup);
        Instance = this;
        motor = GameObject.FindGameObjectWithTag("Player").GetComponent<PlayerMotor_GameLevel>();
        coinText.text = coinScore.ToString("0");
    }

推荐阅读