类型“ CanvasGroup”的对象已被破坏,但您仍在尝试访问它

时间:2019-07-18 18:53:46

标签: c# unity3d

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

没有太多尝试,因为它很个人化。一种修补程序对我不起作用,因为我找不到确切的位置(由于脚本名称不同。)

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等。

1 个答案:

答案 0 :(得分:1)

将其应用于要在场景之间保留的GameObject。

参考:

var gameObject = //whatever
DontDestroyOnLoad(gameObject);

例如:

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