Common Scripting Pitfalls

From Unify Community Wiki
(Difference between revisions)
Jump to: navigation, search
 
Line 8: Line 8:
 
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!
 
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.  Now we are not sending ''SomeCoolFunction'' any object, but we are not requiring the message to have a receiver.
+
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.
 
This is a common pitfall when writing code that is messaging objects that may not have a receiver.

Revision as of 01:37, 28 April 2006

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
Namespaces

Variants
Actions
Navigation
Extras
Toolbox