为了账号安全,请及时绑定邮箱和手机立即绑定

“GameObject”类型的对象已被销毁,但您仍在尝试访问它

“GameObject”类型的对象已被销毁,但您仍在尝试访问它

C#
30秒到达战场 2021-11-21 09:59:52
所以我正在开发一款跑酷游戏,它几乎完成了。问题是我正在测试暂停面板,当玩家触摸僵尸时,暂停面板出现,我通过按下重新启动按钮再次重新启动游戏。但是当我再次触摸僵尸面板时,面板没有出现并给我标题中的错误。我被卡住了,任何帮助将不胜感激。这是代码,我引用了错误发送给我的行:[SerializeField]private GameObject pausePanel;[SerializeField]private Button RestartGameButton;[SerializeField]private Text ScoreText;private int score;void Start (){    pausePanel.SetActive(false);    ScoreText.text = score + "M";    StartCoroutine(CountScore());}IEnumerator CountScore(){    yield return new WaitForSeconds(0.6f);    score++;    ScoreText.text = score + "M";    StartCoroutine(CountScore());}void OnEnable(){    PlayerDeath.endgame += PlayerDiedEndTheGame;}void OnDisable(){    PlayerDeath.endgame += PlayerDiedEndTheGame;}void PlayerDiedEndTheGame(){    if (!PlayerPrefs.HasKey("Score"))    {        PlayerPrefs.SetInt("Score", 0);    }    else    {        int highscore = PlayerPrefs.GetInt("Score");        if(highscore < score)        {            PlayerPrefs.SetInt("Score", score);        }    }    pausePanel.SetActive(true); //this is the line that error sends me but I cant figure it out because I didnt try to destroy the panel in the first place.    RestartGameButton.onClick.RemoveAllListeners();    RestartGameButton.onClick.AddListener(() => RestartGame());    Time.timeScale = 0f;}public void PauseButton(){    Time.timeScale = 0f;    pausePanel.SetActive(true);    RestartGameButton.onClick.RemoveAllListeners();    RestartGameButton.onClick.AddListener(() => ResumeGame());}public void GoToMenu(){    Time.timeScale = 1f;    SceneManager.LoadScene("MainMenu");}public void ResumeGame(){    Time.timeScale = 1f;    pausePanel.SetActive(false);}public void RestartGame(){    Time.timeScale = 1f;    SceneManager.LoadScene("Gameplay");}
查看完整描述

1 回答

?
动漫人物

TA贡献1815条经验 获得超10个赞

我找到了解决方案。这是 OnDisable 方法的一个简单错误。我删除了 += 符号并用 -= 符号更改了它,因为它在应该禁用时启用了该事件。


查看完整回答
反对 回复 2021-11-21
  • 1 回答
  • 0 关注
  • 309 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信