[英]The object of type 'GameObject' has been destroyed but you are still trying to access it
So I am working on a runner game and it is almost done. 所以我正在做一个亚军游戏,差不多完成了。 The problem is that I am testing the Pause Panel and when player touches a zombie, pause panel shows up and I am restarting the game again by pressing the restart button. 问题是我正在测试“暂停面板”,当玩家触摸僵尸时,会出现暂停面板,并且我会通过按“重新启动”按钮来重新启动游戏。 But when I touch the zombie again panel doesnt show up and gives me the error in the title. 但是当我再次触摸僵尸面板时,没有出现,并给了我标题错误。 I am stuck and any help will be appreciated. 我被困住了,任何帮助将不胜感激。 This is the code and I referenced to the line that error sends me : 这是代码,我引用了错误发送给我的那一行:
[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");
}
I found the solution. 我找到了解决方案。 It was a simple mistake on the OnDisable method. 这是OnDisable方法上的一个简单错误。 I deleted the += sign and changed it with -= sign because it was enabling the event when it was supposed to disable. 我删除了+ =符号,并用-=符号对其进行了更改,因为它在应该禁用的情况下启用了该事件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.