Common Scripting Pitfalls

From Unify Community Wiki
(Difference between revisions)
Jump to: navigation, search
(Reworded, changed list to unordered)
Line 1: Line 1:
 
== SendMessage and BroadcastMessage ==
 
== SendMessage and BroadcastMessage ==
See these error messages before?
+
When sending messages, you may find that you sometimes receive the following error messages, even though you're already using '''SendMessageOptions.DontRequireReceiver''' to indicate that the message doesn't need to be implemented:
# '''SendMessage (someFunction) has no receiver!'''
+
*'''SendMessage ''SomeCoolFunction'' has no receiver!'''
# '''BroadcastMessage (someFunction) has no receiver!
+
*'''BroadcastMessage ''SomeCoolFunction'' has no receiver!'''
  
This tip could be for you.  Now, what is the difference between these two lines of code?
+
This may be caused by lines of code such as the following:
  
# <csharp>someGameObject.SendMessage("SomeCoolFunction", SendMessageOptions.DontRequireReceiver);</csharp>
+
<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!
+
It looks correct, but in fact, we're calling the wrong version of '''GameObject.SendMessage()'''.  Instead of telling it that we don't require a receiver, we're actually sending the value '''SendMessageOptions.DontRequireReceiver''' to '''SomeCoolFunction()''' as its function parameter.
  
To correct this, we add the ''null'' argument in the middle as illustrated in the second line of codeNow we are not sending ''SomeCoolFunction'' any object, but we are not requiring the message to have a receiver.
+
The correct form of this method call looks like this:
 +
 
 +
<csharp>someGameObject.SendMessage("SomeCoolFunction", null, SendMessageOptions.DontRequireReceiver);</csharp>
 +
 
 +
This time, we're calling the version of '''GameObject.SendMessage()''' which has three parameters, and the second parameter is set to 'null'That means that 'null' is passed to '''SomeCoolFunction()''' and the '''SendMessageOptions.DontRequireReceiver''' option is correctly interpreted by '''SendMessage'''.
  
 
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 08:53, 28 April 2006

SendMessage and BroadcastMessage

When sending messages, you may find that you sometimes receive the following error messages, even though you're already using SendMessageOptions.DontRequireReceiver to indicate that the message doesn't need to be implemented:

  • SendMessage SomeCoolFunction has no receiver!
  • BroadcastMessage SomeCoolFunction has no receiver!

This may be caused by lines of code such as the following:

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

It looks correct, but in fact, we're calling the wrong version of GameObject.SendMessage(). Instead of telling it that we don't require a receiver, we're actually sending the value SendMessageOptions.DontRequireReceiver to SomeCoolFunction() as its function parameter.

The correct form of this method call looks like this:

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

This time, we're calling the version of GameObject.SendMessage() which has three parameters, and the second parameter is set to 'null'. That means that 'null' is passed to SomeCoolFunction() and the SendMessageOptions.DontRequireReceiver option is correctly interpreted by SendMessage.

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