Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

[JS] What is wrong in this simple code?

Discussion in 'Scripting' started by Mikibey, Mar 9, 2015.

  1. Mikibey

    Mikibey

    Joined:
    Feb 20, 2015
    Posts:
    5
    Hello,i am new to javascript and unity.
    I wrote a simple code for a menu,when the game starts it needs to wait for 5 secconds and then enable the menu with the play button.
    I am using sprites for the menu and the buttons,and this is my code:

    Code (JavaScript):
    1.  
    2. #pragma strict
    3. var menuloaded : int = 0; // Integer bc i like to work with numbers not statements like true/false
    4.  
    5. function Awake () {
    6. if(menuloaded == 0){ //checks if menuloaded == 0 if yes it will disable the menu
    7.     GameObject.Find("backgroundmeniu").GetComponent<SpriteRenderer>().enabled = false;
    8.     GameObject.Find("newgamebuton").GetComponent<SpriteRenderer>().enabled = false;
    9.     yield WaitForSeconds (5); //It wil wait for 5 secconds
    10.     Debug.Log ("I've waited 5 secconds,i am setting the menuloaded to 1 and the i will wait 5 secconds again.", gameObject); //just a debug
    11.     //I still need to set the int to 1,but i have this error.
    12.     }
    13. if(menuloaded == 1){ //If menuloaded == 1 then wait 5 secconds and enable buttons.
    14.     Debug.Log ("I am waiting 5 more secconds then i am enabling the menu", gameObject);
    15.     yield WaitForSeconds (5);
    16.     GameObject.Find("backgroundmeniu").GetComponent<SpriteRenderer>().enabled = true;
    17.     GameObject.Find("newgamebuton").GetComponent<SpriteRenderer>().enabled = true;
    18.     }
    19. }
    20.  
    21. function Start () {
    22.  
    23. }
    24.  
    25. function Update () {
    26. }
    I am getting those errors :
    Assets/Scripts/Meniu/MenuFlashScript.js(6,73): BCE0043: Unexpected token: ).
    Assets/Scripts/Meniu/MenuFlashScript.js(6,74): BCE0044: expecting ), found '.'.
    Assets/Scripts/Meniu/MenuFlashScript.js(6,75): UCE0001: ';' expected. Insert a semicolon at the end.
    Assets/Scripts/Meniu/MenuFlashScript.js(7,70): BCE0043: Unexpected token: ).
    Assets/Scripts/Meniu/MenuFlashScript.js(7,71): BCE0044: expecting ), found '.'.
    Assets/Scripts/Meniu/MenuFlashScript.js(7,72): UCE0001: ';' expected. Insert a semicolon at the end.
    and so on...i have like 12 of those.

    Why are these errors here?Can someone tell me what i wrote wrong? I've checked the spelling and it seems right but i get these errors.
    I am using unity 4.5.0

    Thanks.
     
  2. Ilingis

    Ilingis

    Joined:
    Dec 13, 2012
    Posts:
    63
    GameObject.Find("backgroundmeniu").GetComponent<SpriteRenderer>().enabled=false;

    You have to store this in a variable, if I'm not mistaken.

    var srend : SpriteRenderer = GameObject.Find("backgroundmeniu").GetComponent(SpriteRenderer);
     
    Mikibey likes this.
  3. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    You've got C# syntax in your code.
    You have to use GetComponent(Type) instead of GetComponent<Type>().

    You don't have to, although finding an object and directly accessing members is pretty risky without to check whether something has been found or not. (Well, that does not only apply to GameObject.Find(), but it's a good example as it's very error prone due to typos).
     
  4. Mikibey

    Mikibey

    Joined:
    Feb 20, 2015
    Posts:
    5
    thanks for the help guys.