Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
1
Question by skoandi · May 14, 2013 at 03:17 PM · c#performancesendmessagesingletonfast

Are singletons in C# faster than SendMessage/GetComponent?

I've read some things on Answers about SendMessage being relatively slow... But recently I found out a cool way to communicate between scripts using singetons, for example:

 static public scriptName Instance;
 
 public int doSomething{
     set{
         code goes here
     }
 }

I can call this from any script!

I'm making a mobile game right now so performance is important to me

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

1 Reply

· Add your reply
  • Sort: 
avatar image
3
Best Answer

Answer by iwaldrop · May 14, 2013 at 03:54 PM

Yes, using a singleton or an event will be much faster than using SendMessage or GetComponent. The reason is that both of the latter options traverse every component in the gameobject in order to find either a method to call (via reflection which is slower than making a method call), or to find he component desired. Two things you can do to avoid this is to, as you suggested, use a singleton, or cache a reference to the component, as such:

 SomeComponent cachedComponent = GetComponent< SomeComponent >();

Then you can call a method on that component via:

 cachedComponent.SomePublicMethod();

You can also look into an event model to allow several classes to respond to an event that occurs. Please search google for 'c# events' for more information. In closing, avoid send message. It's not only slow, but it's difficult to follow a code path because it will call a message who h matches a string name on the game object you call it on. There is no way to know which component that method is on, and when others look at your code it's like a dead end without some manual hunting.

Comment
Add comment · Show 5 · 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 skoandi · May 14, 2013 at 03:59 PM 0
Share

Thanks for your reply!

btw found this: Events, Delegates & Singletons

avatar image numberkruncher · May 14, 2013 at 04:19 PM 0
Share

imo caching references of components is way cleaner than using the singleton pattern.

avatar image skoandi · May 14, 2013 at 04:25 PM 0
Share

but the downside is that if you create a new script, you'll have to create new variables n stuff

avatar image numberkruncher · May 14, 2013 at 04:47 PM 0
Share

@skoandi The downside with singleton is that you become dependent upon static data which often makes debugging issues far harder. Take a read through the following Q&A: http://stackoverflow.com/questions/137975/what-is-so-bad-about-singletons

avatar image skoandi · May 14, 2013 at 04:54 PM 0
Share

aha thanks! :)

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

15 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Script Efficiency 1 Answer

Low Priority Methods In LateUpdate? 1 Answer

byte[] to Texture kills performance 3 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