实例化多个GameObject预制件并删除原始对象后,Unity中缺少参考异常

时间:2019-06-06 00:58:42

标签: c# unity3d game-physics

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

只要原始球仍在场景中,但删除场景中的原始球后,一切都可以正常工作(击中屏幕底部的球将被销毁,但如果克隆的球仍然存在,则游戏将继续进行在游戏中并且“球计数”不为0)在两次和三次加电情况下,我在包含实例化功能的行中得到以下错误:

  

MissingReferenceException:'Transform'类型的对象已被破坏,但您仍在尝试访问它。

我的Paddle Code部分处理在paddle和powerup之间发生碰撞时的电源,如下所示:

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;

    }

我不知道这将是一个好的解决方案,并且感谢任何想法!

1 个答案:

答案 0 :(得分:3)

我看不到销毁球的代码或您的设置,但在您的问题中,听起来像PrefabBall中您引用了场景中的原始球。 / p>

因此,当您Destroy()时,PrefabBall当然不会null


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


或者仅禁用“丢失”的球

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始终是最新的。

而且,您可以轻松获得当前的全局球数:

YourScriptType.ArrayOfBalls.Count
相关问题