- Home /
Empty gameobject instantiation problem
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
Ben -- basically DO NOT USE "new GameObject"
that is only for making literally the component which is called "GameObject". it would only be for "technical use"
If you want to take a THING in your game (like "a monster" or "the spaceship") and make more of them, use Instantiate.
there is great confusion in Unity between "a game object" (ie, simply talking about "some THING" in your game) and literally a GameObject which is a special, um, technical thingy - heh.
I've realized another source of confusion here
new GameObject("CarrierControl")
that just makes a new blank GameObject and happens to NA$$anonymous$$E it "CarrierControl"
it has utterly nothing to do with anything in your game that is currently called CarrierControl. okie dokey?
I'm guessing you want to COPY something you already have which is named CarrierControl. If so, you use Instantiate to do that.
Answer by AlucardJay · Dec 27, 2012 at 02:05 PM
Yep, you are creating 2 gameObjects =]
Instantiate is normally used for prefabs : http://docs.unity3d.com/Documentation/ScriptReference/Object.Instantiate.html
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?
=]
Answer by advancexz · Oct 25, 2016 at 05:11 PM
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);
Your answer
![](https://koobas.hobune.stream/wayback/20220613090007im_/https://answers.unity.com/themes/thub/images/avi.jpg)