我需要实例化并销毁一个预制件。我尝试了这些:

public Transform prefab;     //I attached a prefab in Unity Editor

Object o = Instantiate(prefab);
//using this I cannot get the transform component (I don't know why) so useless

Transform o=(Transform)Instantiate(prefab);
//gives transform and transform component cannot be destroyed

GameObject o=(GameObject)Instantiate(prefab);
//invalid cast

那么该怎么做呢?

最佳答案

您不必将实例声明为对象,如果您获得的祖先对象没有转换组件。

public GameObject prefab;
GameObject obj = Instantiate(prefab);

如果要获取转换组件,只需键入obj.transform
如果要销毁对象,请键入Destroy(obj);

关于c# - 实例化并销毁Unity3D,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/18176587/

10-15 09:16