Unity3D 实例化为 GameObject
Unity3D Instantiating as GameObject
My code look like that:
if (Input.GetButtonDown ("Fire2")) {
GameObject transparent = Instantiate (building, new Vector3 (0, -10,0), Quaternion.identity) as GameObject;
}
Where building is public GameObject which I add through unity
Inpsector. After using right click GameObject is instantiated, but
transparent variable has null instead of the instantiated GameObject.
If I change type of transparent to Object and I remove 'as GameObject'
cast, everything works good.
好的,我找出问题所在。我的建筑不是 GameObject,而是 Building(class 继承了 MonoBehaviour,所以它本身就有游戏对象)。现在我实例化building.gameobject,一切正常。
原来我发起'building'作为一个建筑class(建筑是我自己的class并继承了MonoBehaviour),而不是作为一个GameObject,所以统一有问题把建筑进入游戏对象。现在看起来像这样:
if (Input.GetButtonDown ("Fire2")) {
GameObject transparent = Instantiate (building.gameobject, new Vector3 (0, -10,0), Quaternion.identity) as GameObject;
}
My code look like that:
if (Input.GetButtonDown ("Fire2")) { GameObject transparent = Instantiate (building, new Vector3 (0, -10,0), Quaternion.identity) as GameObject; }
Where building is public GameObject which I add through unity Inpsector. After using right click GameObject is instantiated, but transparent variable has null instead of the instantiated GameObject. If I change type of transparent to Object and I remove 'as GameObject' cast, everything works good.
好的,我找出问题所在。我的建筑不是 GameObject,而是 Building(class 继承了 MonoBehaviour,所以它本身就有游戏对象)。现在我实例化building.gameobject,一切正常。
原来我发起'building'作为一个建筑class(建筑是我自己的class并继承了MonoBehaviour),而不是作为一个GameObject,所以统一有问题把建筑进入游戏对象。现在看起来像这样:
if (Input.GetButtonDown ("Fire2")) {
GameObject transparent = Instantiate (building.gameobject, new Vector3 (0, -10,0), Quaternion.identity) as GameObject;
}