How do you do. I want to create a new gameobject and then add the gameobject in scene.
How do I do this?
My code is:
GameObject a = new GameObject();
GameObject aClone = Instantiate(a) as GameObject;
but doesn't work correctly.
The correct way:
GameObject obj = Instantiate(prefab) as GameObject;
You can specify the position
and the rotation
as well.
Vector3 position = new Vector3(1, 1, 1);
Quaternion rotation = new Quaternion(1, 1, 1, 1);
GameObject obj = Instantiate(prefab, position, rotation) as GameObject;
Obviously use the position
and rotation
that you like by changing the parameters.
A prefab is simply:
public GameObject prefab;
Drag a GameObject
into the script via the Editor.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With