简体   繁体   English

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

[英]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.

相关问题 unity 'GameObject' 类型的 object 已被破坏,但您仍在尝试访问它 - unity The object of type 'GameObject' has been destroyed but you are still trying to access it 错误 MissingReferenceException:“GameObject”类型的 object 已被破坏,但您仍在尝试访问它 - Error MissingReferenceException: The object of type 'GameObject' has been destroyed but you are still trying to access it 如何修复 Unity 中的“&#39;GameObject&#39; 类型的对象已被销毁,但您仍在尝试访问它”错误? - How to fix "the object of type 'GameObject' has been destroyed, but you are still trying to access it" error in Unity? 如何修复“'GameObject' 类型的 object 已被破坏,但您仍在尝试访问它。” 统一 - How I can fix the “The object of type 'GameObject' has been destroyed but you are still trying to access it.” Unity “CanvasGroup”类型的对象已被销毁,但您仍在尝试访问它 - The object of type "CanvasGroup" has been destroyed but you are still trying to access it Brackeys 2d platformer GameObject&#39;已被破坏,但您仍在尝试访问它 - Brackeys 2d platformer GameObject' has been destroyed but you are still trying to access it MissingReferenceException:类型为“攻击者”的对象已被破坏,但您仍在尝试访问它 - MissingReferenceException: The object of type 'Attacker' has been destroyed but you are still trying to access it 文本类型的 object 已被破坏,但您仍在尝试访问它 - Unity - The object of type Text has been destroyed but you are still trying to access it - Unity Unity “GameObject”类型的 object 已被销毁 - Unity The object of type “GameObject” has been destroyed “healthscript”类型的游戏 object 已被销毁,但您正在尝试访问它 - The game object of type “healthscript” has already been destroyed but you are trying to access it
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM