Common Scripting Pitfalls

From Unify Community Wiki
Revision as of 01:37, 28 April 2006 by Aarku (Talk | contribs)

Jump to: navigation, search

SendMessage and BroadcastMessage

What is the difference between these two lines of code?

  1. <csharp>someGameObject.SendMessage("SomeCoolFunction", SendMessageOptions.DontRequireReceiver);</csharp>
  2. <csharp>someGameObject.SendMessage("SomeCoolFunction", null, SendMessageOptions.DontRequireReceiver);</csharp>

In the first line of code, we are sending somegameobject the message "SomeCoolFunction" along with the object SendMessageOptions.DontRequireReceiver. That is most likely not what we want!

To correct this, we basically add the null argument in the middle as illustrated in the second line of code. Now we are not sending SomeCoolFunction any object, but we are not requiring the message to have a receiver.

This is a common pitfall when writing code that is messaging objects that may not have a receiver.

Personal tools