Game Development Reference
In-Depth Information
Classified intel
In the CharacterControl script, we will see that we have used BroadcastMes-
sage("Fire", playbackTime) . We will learn what the BroadcastMessage()
function actually does.
BroadcastMessage
The BroadcastMessage() function basically calls all the functions named Fire in
this game object or any of its children. This is a great way to make our script and object
more organized.
Note
Performance-wise, BroadcastMessage() is slower than a function call because it iter-
ates through all the possible target objects, finds matches of the desired function, and ex-
ecutes them. Therefore, it won't cause a huge increase in performance if we don't have a
large number of function calls.
We can have different scripts attached to the children of this parent object and trigger them
at the same time. For example, BroadcastMessage("Fire", playbackTime)
will call the Fire(var f:float) or Fire(float f) function in each component
attached to the object (irrespective of whether we're calling it on Component or GameOb-
ject ). So, when the user clicks on fire , we will have the rocket shot at the same time with
the smoke coming out from the launcher without having to code everything in one big
script. We can see more details at the following links:
http://docs.unity3d.com/Documentation/ScriptReference/Compon-
ent.BroadcastMessage.html
http://docs.unity3d.com/Documentation/ScriptReference/GameOb-
ject.BroadcastMessage.html
Tip
If we use C#, we can avoid using the BroadcastMessage() function by using del-
egate and event to trigger all the objects that have an event listener attached. We will
talk about delegate and event in Project 7 , Forge a Destructible and Interactive Virtu-
al World .
Search WWH ::




Custom Search