如何从统一层次结构中摧毁游戏对象

时间:2022-06-01 17:13:13

I've created the below function to instantiate to game object, but I have to delete it one by one from hierarchy. How can I do that?

我创建了以下函数来实例化游戏对象,但是我必须从层次结构中逐个删除它。我怎样才能做到这一点?

public void button() {
  Instantiate (player1 [i]);
  i = i + 1;

  destroy();
}

1 个解决方案

#1


0  

Destroy wants to know what to destroy so you will need to give it a GameObject too destroy. In your case you can save the GameObject that you instantiate too a variable and than delete it.

Destroy想要知道要销毁什么,所以你需要给它一个GameObject太破坏了。在您的情况下,您可以保存您实例化变量而不是删除它的GameObject。

GameObject player;
player = Instantiate (player[i]) as GameObject;

Destroy(player, 2f); // And now we can destroy the player after 2 seconds

https://docs.unity3d.com/2017.2/Documentation/ScriptReference/Object.Destroy.html

https://docs.unity3d.com/2017.2/Documentation/ScriptReference/Object.Destroy.html

https://docs.unity3d.com/ScriptReference/Object.Instantiate.html

https://docs.unity3d.com/ScriptReference/Object.Instantiate.html

#1


0  

Destroy wants to know what to destroy so you will need to give it a GameObject too destroy. In your case you can save the GameObject that you instantiate too a variable and than delete it.

Destroy想要知道要销毁什么,所以你需要给它一个GameObject太破坏了。在您的情况下,您可以保存您实例化变量而不是删除它的GameObject。

GameObject player;
player = Instantiate (player[i]) as GameObject;

Destroy(player, 2f); // And now we can destroy the player after 2 seconds

https://docs.unity3d.com/2017.2/Documentation/ScriptReference/Object.Destroy.html

https://docs.unity3d.com/2017.2/Documentation/ScriptReference/Object.Destroy.html

https://docs.unity3d.com/ScriptReference/Object.Instantiate.html

https://docs.unity3d.com/ScriptReference/Object.Instantiate.html