- Home /
Instantiate object
I'm stucked with a next problem: I have a prefab "TestObject" and I need to instantiate it in runtime (it doesnt exist in the game initially). From all examples I had seen there are only one example given to everyone:
public GameObject prefab;
//...
Instantiate(prefab, new Vector3(x,y,z), Quaternion.identity) as GameObject;
//...
But I don't know what prefab will be created so I can't assign any value to it. Basically what I would like to do is create predefined cube and after say to it: "you gonna use model X". Is that possible?
You're saying two different and conflicting things:
"I have a prefab "TestObject" and I need to instantiate it in runtime"
and
"But I don't know what prefab will be created so I can't assign any value to it."
Which is it? You have a prefab, or you don't? If you do have a prefab, then just drag it onto the slot in the inspector.
Sorry, I'm being confused as well already. What I'm trying to say is: prefab is a cube. Need to transform that cube into, lets say, $$anonymous$$night in case A or into a Tree in case B (knight and tree are 3ds models).
Sounds like you just need two prefabs then? Or, you can change the mesh of any object by assigning GetComponent<$$anonymous$$eshFilter>().mesh
.
Answer by husbandofemily · Mar 10, 2012 at 06:49 AM
You need:
var prefab : Transform;
outside your function, and drag what you want instantiating onto it in the inspector, like Eric said.
If you want a cube to "change into" your prefab, you'd have to destroy the cube (Destroy (gameObject); ) and use the transform and rotation of said cube in the instantiate Vector3 and rotation.
Hope I understood this right, and that it helps! :)