I ran into a problem with SendMessage. I was trying to make a message based targeting system for a question here at answers.unity3d.com. The problem is that when I attempt to pass a null parameter value into SendMessage, I get this quite unexpected message:
Failed to call function Test of class SendMessageReceiver Calling function Test with no parameters but the function requires 1.
- UnityEngine.Component:SendMessage(String, Object)
- SendMessageSender:Main() (at Assets/SendMessageNullTest/SendMessageSender.js:1)
I want to be able to send null values to clear out the current target but obviously this won't work for now. I sent a bug report to Unity3D. For the sake of simplicity, which workaround do you think is best, and why?
- Add a new func called SetTarget that doesn't accept any params and handle null case.
- Add a new func called ClearTarget that doesn't accept any params thats called when target is null.
- Add a new type called TargetEvent that provides the value and pass that to SendMessage instead.
- Other workaround I haven't thought of?
Below is the culprit code.
SendMessageSender.js
SendMessage("Test", null);
SendMessageReceiver.js
// This function isn't even called...
function Test(actual : GameObject) {
if (actual == null)
print ("Test PASSED!");
else
print ("Test FAILED! (Expected null)");
}