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

时间:2017-11-11 10:20:22

标签: hierarchy

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

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

  destroy();
}

1 个答案:

答案 0 :(得分:0)

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/ScriptReference/Object.Instantiate.html