我该如何解决“类型为'GameObject'的对象已被破坏,但您仍在尝试访问它。”

时间:2019-12-13 23:56:44

标签: unity3d

我的游戏中的对撞机有问题。在控制台上,我收到以下错误消息:MissingReferenceException:'GameObject'类型的对象已被破坏,但您仍在尝试访问它。

我的代码是:

private void OnTriggerEnter2D(Collider2D collision)
{
   if (collision.tag.Equals("Money"))
    {
        if (gameObject != null)
            Score.moneyAmount += 1;
        Destroy(collision.gameObject);
    }

}

我试图将gameObject设置为null,但无法正常工作。 有人可以帮忙吗?

如果有帮助,这就是所有错误: MissingReferenceException: The object of type 'GameObject' has been destroyed but you are still trying to access it. Your script should either check if it is null or you should not destroy the object. UnityEngine.Object.Internal_InstantiateSingle (UnityEngine.Object data, UnityEngine.Vector3 pos, UnityEngine.Quaternion rot) <0x1505ad3c0 + 0x0006a> in <326982e5f7d84df7b8fe3152df5f407e>:0 UnityEngine.Object.Instantiate (UnityEngine.Object original, UnityEngine.Vector3 position, UnityEngine.Quaternion rotation) (at /Users/builduser/buildslave/unity/build/Runtime/Export/UnityEngineObject.bindings.cs:211) UnityEngine.Object.Instantiate[T] (T original, UnityEngine.Vector3 position, UnityEngine.Quaternion rotation) (at /Users/builduser/buildslave/unity/build/Runtime/Export/UnityEngineObject.bindings.cs:285) moneyfall.Update () (at Assets/Scripts/moneyfall.cs:24)

和moneyfall.cs上的第24行是: case 2: Instantiate(prefab2, transform.position, Quaternion.identity); break;

0 个答案:

没有答案