- Home /
Sending and Receiving Messages globally with BroadcastMessage / SendMessage
Hi all.
Looking at BroadcastMessage and SendMessage it appears that only the GameObject or its children can receive the messages. I wish to have a single Event Gameobject in the scene sending messages at intervals. I would like any objects active in the scene to be able to subscribe or "listen" to these messages. Is this possible?
I really like how event bubbling works in Actionscript 3 and would like to mimic this in Unity.
Thanks,
Dave
Answer by boymeetsrobot · Oct 26, 2010 at 07:36 AM
Thanks for the response.
I have found that there are a few event managers that have been written by the community which are a great starting block for writing decoupled code. I would advise to anyone serious about using Unity to look at using these methods of programming as oppose to relying on the hierarchy limitation of the BroadcastMessage approach. In an ideal world, an object should not worry about who is receiving its events or whether it has a relationship or a reference to another object. This leads to code that is not reusable and difficult to debug.
There is an excellent video on the subject by Mike Mittelman of Hangout Industries from Unite 2008 that can be found on Unity3d.com http://unity3d.com/support/resources/unite-presentations/techniques-for-making-reusable-code
Additionally, here is a link to a great Unity forum post that has a few event manager links in it. http://forum.unity3d.com/threads/37896-Programming-architectures I am currently using the iPhone friendly NotificationCenter class by Prime31 and it works in a similar way to the Actionscript 3 event model.
Answer by Eric5h5 · Oct 24, 2010 at 08:54 PM
A simple way is to make an empty gameobject and have all gameobjects that should receive messages be a child of this empty. Then use BroadcastMessage on the empty.
wont it cause an extra matrix calculation whn the object moves or rotates??
Answer by Ghopper21 · Oct 16, 2012 at 10:38 PM
Check out this type-safe event management system by Will Miller.
Answer by dustinandrew · Oct 31, 2012 at 09:30 PM
Event Manager for Unity A static manager for handling an event driven communication model in Unity. This is similar to Adobe Flash's event listener model used in ActionScript.