Hi, when I instantiate a gameobject that I have created dynamically, e.g.
var p : GameObject = GameObject.Instantiate(new GameObject("CarrierControl"), Vector3.zero, Quaternion.identity);
there seems to be 2 instances created - “New Game Object” and “New Game Object(Clone)”.
At the moment it isn’t a problem as I only have a few, but later on when I’m creating more, with lots of components attached, it could get to be a problem.
Anybody got any idea why this is happening? It doesn’t happen when instantiating prefabs, only dynamically created objects.
Thanks
Yep, you are creating 2 gameObjects =]
Instantiate is normally used for prefabs : Unity - Scripting API: Object.Instantiate
new GameObject for making things on-the-fly : docs.unity3d.com/Documentation/ScriptReference/GameObject.GameObject.html
“New Game Object” is your new GameObject(“CarrierControl”)
“New Game Object(Clone)” is your Instantiate
So, pick a method. Do you want to create an object from an object referenced in that script, or do you want to create a new one and add components to it?
=]
ad48hp
3
var p:GameObject = new GameObject();
just drop the ‘new GameObject(“CarrierControl”)’ from your code and use the prefab variable instead:
public Transform prefabToCopy; //maybe assigned from your resources to your script in the component editor, or from resources from within the script
private Transform prefabToSpawn;
prefabToSpawn = Instantiate(prefabToCopy, Vector3.zero, Quaternion.identity);