• 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 /
avatar image
0
Question by losingisfun · Dec 14, 2017 at 11:16 PM · performanceperformance optimizationcomponents

C# - AddComponent Vs GetComponent

Is it more or less costly to use GetComponent() than AddComponent() ?

I've been wondering about this for awhile, haven't seen any documentation or comments to clarify.

For example, if I want an AudioSource component on an Object, inside of a script would it be cheaper to add the component in the scene view, then cache it with GetComponent, or would it be cheaper to add the component on scene loading, and cache it as you create it, e.g. aud = gameObject.AddComponent();

Comment
Add comment · Show 1
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 MaxGuernseyIII · Dec 15, 2017 at 12:35 AM 0
Share

If @Hellium answered your question, I would mark it as such.

1 Reply

· Add your reply
  • Sort: 
avatar image
4

Answer by Hellium · Dec 14, 2017 at 11:54 PM

I believe the difference won't be noticeable except if you call the function multiple times each frame.

I think it's a premature optimization to choose between those two functions.

IMHO, the best is to not call those functions at all and reference them using the inspector.

DO

 // Drag & drop the rigidbody component in the inspector
 [SerializeField]
 private Rigidbody myRigidbody ;


AVOID

 private Rigidbody myRigidbody ;
 
 private void Awake()
 {
     myRigidbody = GetComponent<Rigidbody>();
 }


Comment
Add comment · Show 4 · 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 MaxGuernseyIII · Dec 15, 2017 at 12:34 AM 1
Share

I wanted to upvote you a second time for this but, alas. We each only get one vote.

avatar image Bunny83 · Dec 15, 2017 at 02:54 AM 2
Share

When i saw the question 3 hours ago i thought about writing exactly the same answer but then did something else. Great, now i don't have to write my own answer ^^.

avatar image MaxGuernseyIII Bunny83 · Dec 15, 2017 at 03:22 AM 0
Share

https://www.youtube.com/watch?v=1Toy$$anonymous$$9dhl7g

avatar image Hellium · Dec 15, 2017 at 08:08 AM 1
Share

@losingisfun: As a general rule of thumb, I tend to do as much as possible in the editor. If the designers I work with are able to do the job (meaning, modifying component's properties), it's less job for me.

$$anonymous$$oreover, supposing you work with Interfaces, it's harder to add a component implementing the interface using AddComponent than referencing it through the inspector (or retrieving them using GetComponent)

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

92 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

Related Questions

More than 20 instances of a certain script cause fps to skyrocket, is there something I'm doing wrong here? 0 Answers

Collision Killing CPU/FPS/Performance - Suggestions?! 0 Answers

Figuring out which specific objects are slow to render? 0 Answers

How to implement Jobs?,How can I increase performance with Jobs? 0 Answers

Problem with the performance when activating gameObject 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