gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 22:33:05 25 4
gpt4 key购买 nike

我目前正在制作 Breakout 风格的游戏,并且有 2 个“Power-ups”给我带来了一些麻烦。我有一个双球和三球 Prop ,可以从原始球预制件中实例化 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.

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


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

关于c# - 实例化多个 GameObject 预制件并删除原始文件后,Unity 中缺少引用异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56469762/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com