If I try send a message with null as the param value:
this.SendMessage("OnEmit", null, SendMessageOptions.DontRequireReceiver);
But I get this error:
Failed to call function OnEmit of class BossPartBehaviour
Calling function OnEmit with no parameters but the function requires 1.
UnityEngine.Component:SendMessage(String, Object, SendMessageOptions)
GameObjectEmitter`1:EmitObject() (at Assets/Scripts/GameObjectEmitter.cs:101)
however this call (with a value sent) works :
so the SendMessage lookup does find the method.
this.SendMessage("OnEmit", emittedController, SendMessageOptions.DontRequireReceiver);
The method is defined as:
protected virtual void OnEmit(EmittedObjectController item)
It looks like a null sent as the parameter value makes SendMessage try and do a lookup for a message handler with no parameters.
Could this be a Unity bug?
You can not send null as null is no reference, as such the type can not be gotten from it to find the appropriate function. if you want to send null remove any parameter from the function (-> 2 functions with same name and different header)
I tried that but the OnEmit() method seemed to override the OnEmit(param) method and all messages went to OnEmit() .
Are you saying that I should be able to have:
OnEmit(Type1 param)
OnEmit(Type2 param)
OnEmit(Type3 param)
messages, and depending on what I send via SendMessage’s param value the appropriate message handler should be called?
What I have done for now is have OnEmit(param) and OnEmitNothing() methods. Separate names resolve the current issue.