Variable problem in Javascript.

Hello, so Im learning Unity, and I tried writing flashlight pickup script - everything works great! Except I thought of adding a feature that doesnt let you to pick up multiple flashlight.
So here is the code I need help with:

var flight;
flight = 0;
function OnTriggerEnter (other : Collider)
    {
    if(flight == 0)
      {
      gameObject.collier.enabled = false;
      gameObject.renderer.enabled = false;
      gameObject.light.enabled = false;
      flight = 1;
      }
    }

Ok, so the problem is if(flight == 0) does not seem to have any influence when picking second flashlight it just picks both of them no matter what.
I know that flight is being set to 1, because I tried adding Debug.Log(flight) to the script and it does set it to 1, but second flashlight still can be picked.
I tried setting flight to 1 at the start of the script where flight = 0 is and then it wont pick it up, so I`m really confused. Please help.

Also until I`m here I have few more questions for you people to maybe help me with, ok here it is:

  1. As you can see Im disabling gameObject.collider and etc instead of destroying it, because Ill need it to re-enable in certain amount of time, so is there any shorter way to disable everyting something like gameObject.enabled = false. huh? :smiley:
    (That one does not really matter, Ive just thought I should ask)

  2. How to read variable from another script?
    I know, I know, ive helluva loads of info for it, but I cant still do it.
    So could u please be so kind and help me :stuck_out_tongue:

Okay so as you can see Im using variable flight in my code above (script is called Trigger.js) Im also using the same variable in my other script (flashlight.js) and I would like to instead of creating new variable in Trigger.js to somehow extract it from flashlight.js.
That sounds confusing but I hope u`ll understand me.
Sorry 4 my bad English.
Peace :stuck_out_tongue:

Your problem is that your variable flight is local, meaning that is exists only in that script. When you have another flashlight ITS flight is still 0 even though your original one is 1. A good example would be a health variable. The health only describes the gameobject its attached to, not every single object. You can easily fix this with a static variable.
[https://unity3d.com/learn/tutorials/modules/intermediate/scripting/statics][1]
A static variable is a variable that exists only once across all scripts.

static var flight = 0;
function OnTriggerEnter (other : Collider)
    {
    if(flight == 0)
      {
      gameObject.setEnabled(false);
      flight = 1;
      }
    }

Accessing other scripts is easy also.

GameObject.Find("Name of Object").GetComponent("NameOfScript").variableYouWishToAccess

Good Luck!
[1]: https://unity3d.com/learn/tutorials/modules/intermediate/scripting/statics

You need to add a empty GameObject to your scene, name it something like flashLighVar, make a .js and call it enableFlashlight.js

Then your script above get a reference to the new GameObject flashLighVar

var flashlight : GameObject;

function Start(){

    flashlight = GameObject.Find("flashLighVar");
}

Also in the script add a reference to the ne javascript enableFlashlight.js

var flaslightScript : enableFlashlight;

function Start(){

    flaslightScript = flashlight.GetComponent(enableFlashlight);
}

Then in the enableFlashlight.js add a variable

var allowFlashlight : boolean;

Then reference it

if(flashlightScript.allowFlashlight){
    // allow flashlight to be picked up
    // then set it to false so you can not pick another flashlight up
    flashlightScript.allowFlashlight = false;
}

Not tested, but should help point you in the right direction.

You can achieve it this way. First declare this variable at the top of your Trigger.js

flashlight thisFlashlight;

Then use the GetComponent() function inside your Start() function.

void Start() {
     thisFlashlight = GetComponent<flashlight>();
}

Now you can access the variable through thisFlashlight like the following.

Type myVariable = thisFlashlight.variableName;

Let me know if this helped you.