Tem*_* Id 6 c# unity-game-engine
我需要在运行中实例化并销毁预制件.我试过这些:
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
Run Code Online (Sandbox Code Playgroud)
那怎么办呢?
您不必将您的实例声明为对象,如果您这样做,您将获得没有转换组件的祖先对象。
public GameObject prefab;
GameObject obj = Instantiate(prefab);
Run Code Online (Sandbox Code Playgroud)
如果你想获得变换组件,只需输入obj.transform
。
如果你想销毁对象类型Destroy(obj);
。
归档时间: |
|
查看次数: |
25946 次 |
最近记录: |