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 /
avatar image
0
Question by wulfkub · Jul 01, 2011 at 01:55 AM · optimizationsingletonaddcomponent

Singleton vs AddComponent

The argument, in a nutshell, is that one of us wants to use AddComponent and the other one wants to use a singleton pattern that, in true c# and .Net style, has a single public static reference to itself, allowing access to all public variables and methods.

Obviously, not knowing what AddComponent actually does at a compiler level makes it difficult to determine if there is the potential for a performance hit if we use AddComponent in a bunch of classes instead of just calling the public static accessor and getting the data from that.

So, the question...

What the heck does the compiler do with AddComponent? Is it simply doing the same thing a public static reference does? What potential pitfalls are there and what, if any, performance hits are potentially lurking?

If we were to do a static reference, what potential pitfalls are there and performance hits are possible?

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

· Add your reply
  • Sort: 
avatar image
0

Answer by Dreamora · Jul 01, 2011 at 02:24 AM

AddComponent creates a new instance of the class, adds it to the game object, adds it to the management lists to use "Find" etc.

So compared to a singleton it will always lose the time war, just cause instantiation is by definition slower than an object access.

For your case I would go with a singleton. not needfully one independent of game objects but definitely a unique object per scene / game

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 wulfkub · Jul 01, 2011 at 05:54 AM

Thank you so much for your insight! I looked and looked and could not find anything in the unity docs about AddComponent, other than "you should use it."

As a programmer, the lack of real information unity provides is annoying but at least the comm picks up the slack ;0

Comment
Add comment · Show 1 · 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 testure · Jul 01, 2011 at 06:38 AM 0
Share

Don't post comments as answers please, it messes up search indexing.

avatar image
0

Answer by Dreamora · Jul 01, 2011 at 06:10 AM

Technically you can think of AddComponent as unitys way of creating instances of Component / MonoBehaviour because for neither you must create them through the 'new' keyword, as thats not supported (only GameObjects are 'new-ed')

I'm unsure if the fact on new is in the docs, but I'm pretty sure that somewhere along the docs or the 2D tutorial the relevant facts on what AddComponent is meant for and what it does are mentioned. But I agree that when you come from vanilla c# it can be a bit confusing at first, but once you grasp Unitys more or less pure Component - Pattern workflow it starts to make sense :)

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

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.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Is it costly to get a singleton instance each frame ? 1 Answer

DontDestroyOnLoad 1 Answer

Property vs Field. Which is faster? 1 Answer

I can't use a public non-static int in another script 1 Answer

Singleton Effectiveness 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