Search Unity

Scope Problem : Unity Javascript

Discussion in 'Editor & General Support' started by daholguin, Sep 18, 2014.

  1. daholguin

    daholguin

    Joined:
    Sep 18, 2014
    Posts:
    2
    So I have a basic input of when the player presses the left button, that it sets the variable leftClick = true. I then refer to it from another script through the GetComponent method, but my script basically reads it to be always true even when the player doesn't click. Im new to Javascript within Unity and know that at the very least, that it has be an issue with scope or something that I'm unaware of. Here is the User Input code:

    //IfUserleftclicks..... [moveforward]
    if (Input.GetMouseButtonDown(0) && dead == false) {
    leftClick = true;
    GetComponent.<Rigidbody2D>().velocity.y =3;
    }

    Here is my referncing script :

    var referencedBirdScript:birdScript;
    var newClick: boolean;

    function Start () {

    referenced BirdScript = birdObject.GetComponent.<birdScript>();

    }

    function Update () {


    newClick = referencedBirdScript.leftClick;

    isAlive(); // ignore

    if ( newClick == true ) {
    Debug.Log("DecreasingEnergy");
    if (energy > 0 ) {
    energy -= 3;

    }
    }
    }
     
  2. MysterySoftware

    MysterySoftware

    Joined:
    Sep 18, 2014
    Posts:
    46
    You need to adjust your script to reset 'leftClick' to false when the mouse button isn't pressed.
    Code (JavaScript):
    1. if (Input.GetMouseButtonDown(0) && dead == false) {
    2. leftClick = true;
    3. GetComponent.<Rigidbody2D>().velocity.y =3;
    4. } else
    5. leftClick = false;