C# 团结一致IsDestroying()错误

C# 团结一致IsDestroying()错误,c#,unity3d,destroy,C#,Unity3d,Destroy,在Unity中,我现在遇到了一个以前不存在的错误,或者如果他们犯了错误,这似乎并不重要 每当我销毁某个东西并重新启动游戏关卡时,游戏会在加载过程中冻结,并出现以下错误: !IsDestroying() UnityEngine.GameObject:AddComponent() EggScript:OnDestroy() (at Assets/Scripts/GameScripts/EggScript.cs:85) 它所说的功能是: void OnDestroy() { PlayerPr

在Unity中,我现在遇到了一个以前不存在的错误,或者如果他们犯了错误,这似乎并不重要

每当我销毁某个东西并重新启动游戏关卡时,游戏会在加载过程中冻结,并出现以下错误:

!IsDestroying()
UnityEngine.GameObject:AddComponent()
EggScript:OnDestroy() (at Assets/Scripts/GameScripts/EggScript.cs:85)
它所说的功能是:

void OnDestroy()
{
    PlayerPrefs.SetInt("Game Paused", 1);
    transform.parent.gameObject.transform.parent.gameObject.AddComponent<GameOverScript>();
}
我不太确定我是在哪里创建的,因为我正在研究的是一些教程的集合,但我认为这与这个特定的错误无关

GameOverScript代码如下所示,尽管听起来也不像是错误:

void Start()
{
    GameManagerScript.Instance.pauseGame();
    //gameObject.BroadcastMessage("saveScore", null, SendMessageOptions.DontRequireReceiver);
    if (PlayerPrefs.GetString("curLevel") == "Tutorial" || PlayerPrefs.GetString("curLevel") == "Tutorial1" || PlayerPrefs.GetString("curLevel") == "Tutorial2")
    {
        //Do nothing
    }
    else
    {
        //We're in an infinite level, so we save the score
        gameObject.BroadcastMessage("saveScore");
    }

    PauseScript.displayMenu();
    //gameObject.AddComponent<FrontmostClickCheckerScript>();

    //HighScoreManager._instance.SaveHighScore("You", (System.Int32)PlayerPrefs.GetFloat("LatestScore"));
    HighScoreManager._instance.SaveHighScore("You", GameManagerScript.Instance.getScore());
}

事实上,我相信您可能在这里犯了一些错误:

transform.parent.gameObject.transform.parent.gameObject.AddComponent应替换为一个公共变量,以保存该游戏对象,即其上的调用AddComponent,也可以将AddComponent替换为enabled,如注释中所述

您正在暂停时销毁一个对象。您应该在这里发布更多代码来提供上下文,但您不希望使用OnDestroy来管理游戏状态。您可以使用消息传递或引用,后者速度更快,但如果您不经常使用它,则前者也可以:在OnUpdate中重复使用


我认为最有可能的情况是,您调用AddComponent的对象可能在您尝试添加组件时被破坏。在这里调试将是一件非常困难的事情,因为您正在手动升级层次结构。另外,尝试完全删除AddComponent,听起来您需要改为SetActive。

我怀疑transform.parent.gameObject.transform.parent.gameObject.AddComponent;这是问题的根源。我怀疑您正在访问的GameObject实例在整个销毁过程中被锁定,尽管我并不确定。如果你在OnDestroy中注释掉那一行会怎么样?我没有想到,我仍然可以通过重试按钮重置场景,只需一个Application.LoadLevel,我得到了相同的结果-冻结了游戏-但没有任何明显的错误。我又乱了一点,发现可能是我的例子。我遵循下面的代码:这已经过时了吗?您可以尝试将最初禁用的GameOverScript附加到代码中引用的游戏对象。游戏结束并执行OnDestroy后,通过gameOverScript.enabled=true;启用脚本;。但是,您需要在GameOverScript中编写代码。Start应该移动到另一个公共函数,以便以后可以从您的游戏管理器调用该函数。可靠的建议,听起来与Sockled Trailmix所说的类似。我发现我在实例中遇到的错误是因为我没有在一个可以保持不变的游戏对象下设置我的单例。所以他们的父母被摧毁了,引发了一些问题。我会更新,如果我可以得到使脚本正常工作。似乎再次工作良好,非常感谢你的帮助!这似乎已经解决了这个问题,尽管其中的另一个主要问题是我没有将我的单身者分离成他们自己的游戏对象,这些对象可以在不同的级别/等等中持续存在。我已经改变了很多事情,现在我不确定我是否能够找到问题的根源,但是你的修复肯定是其中的一个重要部分,所以我把它标记为答案。谢谢你的帮助!
void Start()
{
    GameManagerScript.Instance.pauseGame();
    //gameObject.BroadcastMessage("saveScore", null, SendMessageOptions.DontRequireReceiver);
    if (PlayerPrefs.GetString("curLevel") == "Tutorial" || PlayerPrefs.GetString("curLevel") == "Tutorial1" || PlayerPrefs.GetString("curLevel") == "Tutorial2")
    {
        //Do nothing
    }
    else
    {
        //We're in an infinite level, so we save the score
        gameObject.BroadcastMessage("saveScore");
    }

    PauseScript.displayMenu();
    //gameObject.AddComponent<FrontmostClickCheckerScript>();

    //HighScoreManager._instance.SaveHighScore("You", (System.Int32)PlayerPrefs.GetFloat("LatestScore"));
    HighScoreManager._instance.SaveHighScore("You", GameManagerScript.Instance.getScore());
}