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
10
Question by AntLewis · Nov 14, 2011 at 09:19 PM · eventsdelegate

Send/Broadcast message vs Delegates and Events

Hi, looking through the Unity docs I noticed the BroadcastMessage and SendMessage methods. I'm assuming this is Unity's version of C sharp delegates and events? Are there any advantages of using one over the other in terms of performance or usage?

Many thanks!

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
20
Best Answer

Answer by Peter G · Nov 14, 2011 at 11:04 PM

SendMessage and delegates are used for similar purposes, but they work entirely differently. Delegates/Events behave exactly like they do in any other .Net program. SendMessage() is called much like Unity calls other messages like Update(). Unity reflects over all the code on the game object to see if it can invoke any messages.

As far as performance. Delegates are several times faster than SendMessage. In the order of performance:

  (Least) Direct Method Call ==> Delegate/Event ==> Update() ==> SendMessage (Most Expensive)

As far as usage, SendMessage is like a very broad direct method call. The general paradigm is that one script specifically wants to send a message to a specific object. We're just not sure how many times is can respond or if it can respond at all. We know the intended receiver, we just don't know if it can receive the message.

With events, I usually follow the idea that the event owner doesn't know who or how many objects might respond. We are simply telling the world that something happened without too much knowledge of who will use this information.

Comment
Add comment · Show 7 · 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 AntLewis · Nov 18, 2011 at 05:34 PM 0
Share

$$anonymous$$any thanks for the thorough response, much appreciated.

avatar image GeorgeRigato · Dec 23, 2012 at 09:34 PM 0
Share

Also helped me. Thanks.

avatar image Beugnen · Mar 30, 2013 at 02:20 AM 0
Share

Agreed. One thing con of events is that one must subscribe to them and remember to unsubscribe later via "-=" else it can lead to a memory leak. As opposed to Send$$anonymous$$essage which is loosely coupled

avatar image glantucan · Sep 10, 2014 at 06:12 AM 0
Share

@Beugnen No pun intended, I agree with you about the unsuscribing, but I don't think the posibility of a memory has anything to do with coupling. Actually Send$$anonymous$$essage() needs the dispatcher to know the receiver GameObject (unless the dispatcher and the receiver are the same one), which indeed encourage more tightly coupled code. Events are one of the best and cheapest ways of decoupling code.

avatar image mtalbott · Jan 28, 2015 at 04:24 PM 0
Share

@PeterG Your response if very helpful. I have a follow up question. Given that Send$$anonymous$$essage is the slowest and a direct call is the fastest, if I want to try to call a method that may or may not exist in a GameObject, would I get any performance benefit if I use GetComponent, then check if the component exists, and then call the method directly?

Show more comments
avatar image
1

Answer by weavermount · Apr 14, 2015 at 10:20 PM

@Beugenen, the possibility of a memory leak comes from listeners preventing garbage collection. While conceptually "Emitters don't know or care who's listening" they actually do have a direct reference to each listener, this is why they are fast. But this means that listing object won't get garbage collected until either the listener is removed, or the emitter is destroyed. If the listener is some persistent object like a player or worse level loader, then you open the door for a memory leak

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 Mikdad_Merchant · Jun 08, 2015 at 05:53 AM

Well I want to put a new paradigm to this question . Unity now has its own event system is this better performance wise as compared to C sharp events and delegates

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

9 People are following this question.

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

Related Questions

When to use 'delegate', 'event' or 'Action' ? 1 Answer

Singleton class or delegate? 1 Answer

Dynamic Anonymous Delegates/Lambda On UntyEvent 1 Answer

Choosing Events/Delegates to Register for by Parameters Passed 0 Answers

What happens with the receiver when the same event is trigger from multiple instances at the same time? 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