Search Unity

SendMessage will null as value: Method not

Discussion in 'Editor & General Support' started by raygun, Nov 11, 2010.

  1. raygun

    raygun

    Joined:
    Oct 28, 2010
    Posts:
    3
    If I try send a message with null as the param value:
    Code (csharp):
    1. this.SendMessage("OnEmit", null, SendMessageOptions.DontRequireReceiver);
    But I get this error:

    however this call (with a value sent) works:
    so the SendMessage lookup does find the method.

    Code (csharp):
    1. this.SendMessage("OnEmit", emittedController, SendMessageOptions.DontRequireReceiver);
    The method is defined as:
    Code (csharp):
    1. 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?
     
    Last edited: Nov 11, 2010
  2. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    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)
     
  3. raygun

    raygun

    Joined:
    Oct 28, 2010
    Posts:
    3
    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?

     
  4. raygun

    raygun

    Joined:
    Oct 28, 2010
    Posts:
    3
    What I have done for now is have OnEmit(param) and OnEmitNothing() methods. Separate names resolve the current issue.