so i made this script Code (csharp): var inBasket : boolean = false; function OnTriggerEnter (other : Collider) { inBasket = true; StartCoroutine("EndLevel"); } function OnTriggerExit (other : Collider) { inBasket = false; StopCoroutine("EndLevel"); } function EndLevel() { yield WaitForSeconds(3); if (inBasket) { gameObject.Find("Nextlevelblock").SendMessage("React"); gameObject.Find("Nextlvltxt").SendMessage("React"); gameObject.Find("Replaylvltxt").Sendmessage("react"); } } 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): function react () { nxtlvl = true; } function Update () { if(nxtlvl) { renderer.enabled = true; button = true; } else { renderer.enabled = false; } if(button) { gameObject.AddComponent(BoxCollider); } } 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.
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.
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): gameObject.Find("Nextlevelblock").GetComponent(Foo).React();
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): function react () { nxtlvl = true; } function Update () { if(nxtlvl) { renderer.enabled = true; button = true; } else { renderer.enabled = false; } if(button) { gameObject.AddComponent(BoxCollider); } } 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): function React () { renderer.enabled = true; gameObject.AddComponent(BoxCollider); } --Eric
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