Search Unity

broadcast message doesn`t do anything, no errors

Discussion in 'Scripting' started by Rob-Reijnen, Oct 23, 2014.

  1. Rob-Reijnen

    Rob-Reijnen

    Joined:
    Oct 14, 2013
    Posts:
    60
    I have used broadcast message in my game before and it works just as it should.

    So I added a new one that works te same way as the rest, but this one doesn`t do anything.

    Here is my code:

    Code (CSharp):
    1. //This is the broadcast
    2.  
    3. GameObject Controller = GameObject.FindGameObjectWithTag("GameController");
    4.  
    5.                     Controller.BroadcastMessage(j.ActionMethod,float.Parse (p.ActionParameter));
    Code (JavaScript):
    1. //This is the receiver
    2. function Bulletin (BulletStr : String) {
    3.         ShowBullet = true;
    4.         BulletString = BulletStr;
    5.     }
    6.  
    7. //This is what it should do
    8. function OnGUI(){
    9.     if (ShowBullet)
    10.     {
    11.                 GUI.BeginGroup (Rect (Screen.width / 2 - 264, Screen.height / 2 - 106, 528, 212));
    12.                     GUI.DrawTexture(Rect(0,0,528,212), TextBlock);
    13.                     GUI.TextArea(Rect (16+8, 16+24+32,480,96), BulletString);
    14.                  
    15.                     if (GUI.Button(Rect(8, 8+164, 512, 32),"I get it!"))
    16.                     {
    17.                         ShowBullet = false;
    18.                     }
    19.                 GUI.EndGroup ();
    20.     }
    21. }
    I don`t get any error messages, the other broadcasts work the same way and they work fine.

    Can anybody help me? It`s really frustrating.
     
  2. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,694
    This isn't a fix, but a suggestion to help debug. Instead of:
    Code (csharp):
    1. Controller.BroadcastMessage(j.ActionMethod,float.Parse (p.ActionParameter));
    Code (csharp):
    1. var methodName = j.ActionMethod;
    2. var parameter = float.Parse (p.ActionParameter);
    3. Debug.Log("Broadcasting methodName: " + methodName + ", parameter: " + parameter);
    4. Controller.BroadcastMessage(methodName,parameter);
    Maybe there's an error in the value of methodName.
     
  3. Rob-Reijnen

    Rob-Reijnen

    Joined:
    Oct 14, 2013
    Posts:
    60
    Hi, thanks for your reply.

    I tried your code but it didn`t show me any mistakes.
    But i tried sending an integer as a parameter instead of a string, and it worked.
    I guess I can`t send any strings with broadcast message. Perhaps you know why?
     
  4. Diablo404

    Diablo404

    Joined:
    Mar 15, 2013
    Posts:
    136
    I maybe out of th subject, just in case, do you specifically want to use BroadcastMessage? Or any other way to speak to your function will match your needs?
     
  5. Rob-Reijnen

    Rob-Reijnen

    Joined:
    Oct 14, 2013
    Posts:
    60
    no, broadcast message just seemed the most appropiate way to do it.
     
  6. Diablo404

    Diablo404

    Joined:
    Mar 15, 2013
    Posts:
    136
    Okay, so let's say the script where you have your function Bulletin on your GameController is called Reciever.js, so instead of:

    GameObject Controller = GameObject.FindGameObjectWithTag("GameController"); Controller.BroadcastMessage(j.ActionMethod,float.Parse (p.ActionParameter));

    You could call your function like so:

    GameObject.FindWithTag("GameController").GetComponent(Reciever).Bulletin("The bullet message I want to send");
     
  7. Rob-Reijnen

    Rob-Reijnen

    Joined:
    Oct 14, 2013
    Posts:
    60
    okay, is it possible to do something like this?

    GameObject.FindWithTag("GameController").GetComponent(Reciever).j.ActionMethod(float.Parse (p.ActionParameter));

    because I want to be able to change the function in the inspector
     
    Last edited: Oct 24, 2014
  8. Diablo404

    Diablo404

    Joined:
    Mar 15, 2013
    Posts:
    136
    That's why I asked you if you wanted a specific way to do it. I couldn't tell you I've never tried those ActionMethod things