Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This question was closed Jan 12, 2019 at 12:55 PM by ginorme for the following reason:

Too subjective and argumentative

avatar image
0
Question by ginorme · Jan 12, 2019 at 01:30 AM · gameobjectinstantiategetcomponentaddcomponent

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

  • Sort: 
avatar image
1

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.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Bunny83 · Jan 12, 2019 at 09:38 AM 0
Share

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;
avatar image ginorme · Jan 12, 2019 at 09:57 AM 0
Share

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.

avatar image Ady_M · Jan 12, 2019 at 10:02 AM 0
Share

$$anonymous$$y mistake. I hadn't noticed the new GameObject part.

@Bunny83 has explained what's happening.

avatar image
1

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);

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Follow this Question

Answers Answers and Comments

159 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges