Common Scripting Pitfalls
SendMessage and BroadcastMessage
See these error messages before?
- SendMessage (someFunction) has no receiver!
- BroadcastMessage (someFunction) has no receiver!
This tip could be for you. Now, what is the difference between these two lines of code?
- <csharp>someGameObject.SendMessage("SomeCoolFunction", SendMessageOptions.DontRequireReceiver);</csharp>
- <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 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.