Search Unity

Braodcast message with null parameter

Discussion in 'Scripting' started by Ryuuguu, Dec 30, 2007.

  1. Ryuuguu

    Ryuuguu

    Joined:
    Apr 14, 2007
    Posts:
    391
    I want to Broadcst a message and the parameter value is sometimes null.
    Code (csharp):
    1.  
    2. transform.BroadcastMessage("SetTarget",target,SendMessageOptions.DontRequireReceiver)
    3.  
    and sometimes target is null. My SetTarget method can hndle a null case for target but when I try to broadcsst a message where target = null I get
    Failed to call function SetTarget of class LightningBolt
    Calling function SetTarget with no parameters but the function requires 1.
    UnityEngine.Component:BroadcastMessage(String, Object, SendMessageOptions)

    Is this there a way around this other than check if target is null and broadcasting a different message for that case?

    the recier is a cs file and the sender boo if that matters.

    Cheers,
    Grant
    Turret:Update() (at Assets/Scripts/Turret.boo:32)

    (yes I am using a modified version of Jonathan Czeck's awesome lightningbolt)
     
  2. NCarter

    NCarter

    Joined:
    Sep 3, 2005
    Posts:
    686
    I imagine the problem is that if you don't give the optional parameter, it tries to invoke YourMessage() instead of YourMessage(parameter), and if you haven't defined the former you'll get an error.

    You can probably work around that by implementing YourMessage() in both ways and having the version without a parameter invoke the version which has one, like this:

    Code (csharp):
    1. function YourMessage()
    2. {
    3.     YourMessage(null);
    4. }
    5.  
    6. function YourMessage(parameter : Type)
    7. {
    8.     ...
    9. }
    10.