Search Unity

Send message problems

Discussion in 'Scripting' started by Nightshade1, Feb 24, 2012.

  1. Nightshade1

    Nightshade1

    Joined:
    Feb 6, 2011
    Posts:
    95
    so i made this script
    Code (csharp):
    1.  
    2. var inBasket : boolean = false;
    3.  
    4. function OnTriggerEnter (other : Collider) {
    5.     inBasket = true;
    6.     StartCoroutine("EndLevel");
    7. }
    8.  
    9. function OnTriggerExit (other : Collider) {
    10.     inBasket = false;
    11.     StopCoroutine("EndLevel");
    12. }
    13.  
    14. function EndLevel() {
    15.     yield WaitForSeconds(3);
    16.     if (inBasket)
    17.     {
    18.      gameObject.Find("Nextlevelblock").SendMessage("React");
    19.      gameObject.Find("Nextlvltxt").SendMessage("React");
    20.      gameObject.Find("Replaylvltxt").Sendmessage("react");
    21.     }
    22. }
    i made sure in th hierachy that the caps were correct and thespelling was perfect

    and this is the script attached to that one

    Code (csharp):
    1. function react ()
    2. {
    3.     nxtlvl = true;
    4. }
    5. function Update ()
    6. {
    7.     if(nxtlvl)
    8.     {
    9.     renderer.enabled = true;
    10.     button = true;
    11.     }
    12.     else
    13.     {
    14.     renderer.enabled = false;
    15.     }
    16.     if(button)
    17.     {
    18.     gameObject.AddComponent(BoxCollider);
    19.     }
    20. }
    i made debug booleans to see if the message was sending but it wasn't what should i do???


    P.S i know that some of the script is uneeded but it keeps me organized.
     
  2. GargerathSunman

    GargerathSunman

    Joined:
    May 1, 2008
    Posts:
    1,571
    React needs to be capitalized in the script with it as a function. SendMessage also needs to be perfect in capitalization, as one of them has the right capitalization for react, but doesn't capitalize the SendMessage function itself.
     
  3. Diviner

    Diviner

    Joined:
    May 8, 2010
    Posts:
    677
    You shouldn't use SendMessage at all. It's incredibly slow as it searches throughout the entire Component Hierarchy of the target GameObject for the first MonoBehaviour Component that contains the method you send. If you know (and you should) what script contains which method (in your case React), you should use GetComponent instead. Assuming the Script's name that contains React is Foo, you should do

    Code (csharp):
    1.  
    2. gameObject.Find("Nextlevelblock").GetComponent(Foo).React();
    3.  
     
  4. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Well, it's not that slow. If you're only calling it occasionally, it's not an issue. GetComponent is usually the better option, but SendMessage has its uses. There are cases where you don't know what the component type is, and instead of tying yourself in knots trying to get the type, just sending a message is cleaner and easier. (Aside from using strings, but, well, it's still OK.)

    I would be more concerned about things like this:

    Code (csharp):
    1. function react ()
    2. {
    3.     nxtlvl = true;
    4. }
    5.  
    6. function Update ()
    7. {
    8.     if(nxtlvl)
    9.     {
    10.     renderer.enabled = true;
    11.     button = true;
    12.     }
    13.     else
    14.     {
    15.     renderer.enabled = false;
    16.     }
    17.     if(button)
    18.     {
    19.     gameObject.AddComponent(BoxCollider);
    20.     }
    21. }
    22.  
    Instead of making convoluted logic with global variables and constantly checking those variables in Update, don't use Update at all, just run the code in the function as needed.

    Code (csharp):
    1. function React ()
    2. {
    3.     renderer.enabled = true;
    4.     gameObject.AddComponent(BoxCollider);
    5. }
    --Eric
     
  5. Nightshade1

    Nightshade1

    Joined:
    Feb 6, 2011
    Posts:
    95
    Thankyou all i will note all fo this down and bythe way im new to scripting thats why im using var and else statements as such