- Home /
Too subjective and argumentative
GetComponent vs AddComponent
//----------------------------------- Code 1
public spawnerPrefab;
var temp = Instantiate(spawnerPrefab, spawnPoint.position, Quaternion.identity).GetComponent<Spawner_Enemy>();
Foo(temp);
//----------------------------------- Code 2
var temp = Instantiate(new GameObject(), spawnPoint.position, Quaternion.identity).AddComponent<Spawner_Enemy>();
Foo(temp);
Both codes do the same thing, but I want to know which one is better.
Answer by Ady_M · Jan 12, 2019 at 02:31 AM
GetComponent attempts to get a reference to a component that is currently attached to the object. It returns null if it was not found.
AddComponent is used to attach a new component to the object. Calling AddComponent multiple times will attach multipe instances of the component to the object.
You said both codes are working as expected. Try executing Code 2 and then look in the Inspector while the game is running. Does the object have 2 x Spawner_Enemy scripts attached?
Edit:
My apologies. The last part of my answer (about the double scripts) is wrong. I didn't notice the new GameObject part in your example.
You are right, but in his second case he doesn't instantiate the prefab, but he creates a new empty gameobject, which he clones and then attaches the component. new GameObject()
already creates a gameobject in the scene. When using Instantiate you'll end up with two gameobject, one empty and the clone with the component attached. For the second case you would want to use
var temp = (new GameObject()).AddComponent<Spawner_Enemy>();
temp.transform.position = spawnPoint.position;
No, but 2 gameObjects are being instantiate: https://answers.unity.com/questions/1242221/instantiate-creates-two-gameobjects-in-scene.html This post answer my question.
Thx for the reply.
$$anonymous$$y mistake. I hadn't noticed the new GameObject part.
@Bunny83 has explained what's happening.
Answer by Bunny83 · Jan 12, 2019 at 09:48 AM
You have several different things in your two cases which makes it difficult to say which is better. Your first case instantiates a premade and serialized prefab. That means you can preconfigure everything in that prefab asset and the clone will have the same settings as you set them up in the inspector for the prefab.
As i said in the comment, in your second case you actually create two gameobject. new GameObject()
already creates an empty gameobject which Instantiate will clone. You then attach a new "Spawner_Enemy" component to your cloned gameobject. Note that the Spawner_Enemy component will have it's default values. Also keep in mind that a prefab can be a much more complex object with several components and child objects.
Finally your first case can be simplified even more. Instantiate will return the same object type that you pass in. So what it returns depends on the type of your prefab variable. If you declare a GameObject prefab variable, Instantiate will return the GameObject reference of the cloned object.
However you can also declare the prefab variable of type "Spawner_Enemy". (Note if you change the type you have to reassign the prefab in the inspector). When you call Instantiate on a component reference Unity will clone the whole gameobject as well. Though in addition Instantiate will directly return the cloned "Spawner_Enemy" component. So your first case would become:
public Spawner_Enemy spawnerPrefab;
var temp = Instantiate(spawnerPrefab, spawnPoint.position, Quaternion.identity);
Foo(temp);
Answer by badadam · Jan 12, 2019 at 09:59 AM
var temp = Instantiate(spawnerPrefab, spawnPoint.position, Quaternion.identity).GetComponent(); This is the best. Because spawnerPrefab has already component named "Spawner_Enemy" so when this code run, it don't spend time to add the component
Follow this Question
Related Questions
Can't add sprite to instantiated GameObject 2 Answers
How to get a component from an object and add it to another? (Copy components at runtime) 12 Answers
Accessing instantiated GOs nested in prefabs with GetComponent 4 Answers
Photon Instantiate a prefab and add script on it. 1 Answer
Only change a variable on the instaniated object not the prefab. 0 Answers