实例化多个 GameObject 预制件并删除原始文件后,Unity 中缺少引用异常

Missing reference exception in Unity after instantiating multiple GameObject prefabs and deleting original

我目前正在制作 Breakout 风格的游戏,有 2 个 "Power-ups" 给我带来了一些麻烦。我有一个双球和三球道具,可以从原始球预制件中实例化 1 或 2 个额外的球。

一切正常,只要原始球仍在场景中,但在场景中删除原始球后(球在撞击屏幕底部后被销毁,但如果克隆球,游戏继续仍在播放并且 "Ball Count" 不为 0)在双倍和三倍加电情况下,我在包含 Instantiate 函数的行中收到以下错误:

MissingReferenceException: The object of type 'Transform' has been destroyed but you are still trying to access it.

我的桨代码部分处理桨和电源之间的碰撞电源如下:

void OnTriggerEnter2D(Collider2D collision)
{
    GameObject[] ArrayOfBalls = GameObject.FindGameObjectsWithTag("Ball");

    switch (collision.tag)
    {
        case "Life Powerup" :
            Debug.Log("hit " + collision.name);
            gm.UpdateLives(1);
            Destroy(collision.gameObject);
            break;

        case "Speed PowerUp":

            for (int i = 0; i<ArrayOfBalls.Length; i++)
            {
                ball ballscript = ArrayOfBalls[i].GetComponent < ball>();
                if (ballscript.rb.velocity.magnitude > 7.5f)
                {
                    CancelInvoke("NormalSpeed");
                    Invoke("NormalSpeed", 5f);

                }
                else if (ballscript.rb.velocity.magnitude <= 7.5f)
                {
                    ballscript.rb.velocity = new Vector2(ballscript.rb.velocity.x * 1.5f, ballscript.rb.velocity.y * 1.5f);
                    Invoke("NormalSpeed", 5f);
                }

            }

            //If another powerup is collected call to function to slow down is cancelled and started with new delay


            Debug.Log("hit " + collision.name);
            Destroy(collision.gameObject);
            break;

        case "Shrink PowerDown":

            gameObject.transform.localScale = new Vector3( 0.25f, 0.4f, 1f);
            Invoke("NormalSize", 15);

            Debug.Log("hit " + collision.name);
            Destroy(collision.gameObject);
            break;

        case "Expand PowerUp":

            gameObject.transform.localScale = new Vector3( 0.55f, 0.4f, 1f);
            Invoke("NormalSize", 5f);
            Debug.Log("hit " + collision.name);
            Destroy(collision.gameObject);
            break;

        case "Double PowerUp":
            for (int i = 0; i < ArrayOfBalls.Length; i++)
            {
                ball ballscript = ArrayOfBalls[i].GetComponent<ball>();
                Transform Doubleclone = Instantiate(PrefabBall, new Vector3(ballscript.rb.position.x + 0.1f, ballscript.rb.position.y + 0.1f, 0f), Quaternion.identity);
                Doubleclone.GetComponent<Rigidbody2D>().velocity = new Vector2(ballscript.rb.velocity.x, ballscript.rb.velocity.y);
                gm.UpdateBallCount(1);


            }
            Debug.Log("hit " + collision.name);
            Destroy(collision.gameObject);
            break;

        case "Triple PowerUp":
            for (int i = 0; i < ArrayOfBalls.Length; i++)
            {

                ball ballscript = ArrayOfBalls[i].GetComponent<ball>();
                Transform Tripleclone = Instantiate(PrefabBall, new Vector3(ballscript.rb.position.x + 0.1f, ballscript.rb.position.y + 0.1f, 0f), Quaternion.identity);
                Transform Tripleclone2 = Instantiate(PrefabBall, new Vector3(ballscript.rb.position.x - 0.1f, ballscript.rb.position.y - 0.1f, 0f), Quaternion.identity);
                Tripleclone.GetComponent<Rigidbody2D>().velocity = new Vector2(ballscript.rb.velocity.x, ballscript.rb.velocity.y);
                Tripleclone2.GetComponent<Rigidbody2D>().velocity = new Vector2(ballscript.rb.velocity.x, ballscript.rb.velocity.y);
                gm.UpdateBallCount(2);

            }

            Debug.Log("hit " + collision.name);
            Destroy(collision.gameObject);
            break;

    }

我不知道对此有什么好的解决方法,感谢任何想法!

我没有看到球被破坏的代码或你的设置,但在你的问题中,我觉得 PrefabBall 你引用了 Scene[= 中的原始球61=].

所以当你 Destroy() 它然后 PrefabBallnull 当然。


不如在 Assets 中使用实际的预制件而不是引用场景对象怎么样?我猜你没有这样做是因为你直接想克隆它来自通电的当前属性。但是还有其他选择(例如,像使用刚体设置一样复制它们,或者将通电存储为 static 值)


或者只禁用 "lost" 球

ball.gameObject.SetActive(false);

而不是摧毁它们。这样它就完好无损,但不再可用于游戏。您只需要确保启用

之类的实例化球
Doubleclone.gameObject.SetActive(true);

作为旁注,我还想在这里指出一些效率问题

你一直在用

ball ballscript = ArrayOfBalls[i].GetComponent<ball>();

直接使用

会更有效率
ball[] ArrayOfBalls = FindObjectsOfType<ball>();

或者作为 Find 的完整替代品使用

public static readonly List<ball> ArrayOfBalls = new List<ball>();

PrefabBall 的类型设为 ball,现在每次实例化一个新球时都将其添加到列表中

ball Doubleclone = Instantiate(PrefabBall, ....);
ArrayOfBalls.Add(Doubleclone);

并且每次之前Destroy()一个球首先从列表中删除它

ArrayOfBalls.Remove(ball);
Destroy(ball.gameObject);

通过这种方式,您的 ArrayOfBalls 始终是 up-to-date。

你可以很容易地得到当前的全局球数:

YourScriptType.ArrayOfBalls.Count