Press key to toggle pointlight on/off

Hey guys. I tried to make an area were the player can toggle a pointlight on/off. I made a box collider and i enabled the trigger. If you’re inside the box collider. There will come a text with. Press “L” to toggle light. And if the pointlight is on. And you leave the box collider. The pointlight will turn off.

Someone understand this and can help me?

Thanks in advance…

You should set a boolean flag in OnTriggerEnter, and clear it in OnTriggerExit. This boolean flag could be used in OnGUI to enable a GUI.Label with the text, and in Update to enable the light and the Input.GetKeyDown to toggle the light on/off - something like this (trigger script):

var pLight: Transform; // drag the point light here
private var enableLight: boolean = false;

function OnTriggerEnter(col: Collider){
  if (col.tag == "Player") enableLight = true;
}

function OnTriggerExit(col: Collider){
  if (col.tag == "Player") enableLight = false;
}

function OnGUI(){
  if (enableLight){
    GUI.Label(Rect(10,Screen.height-50, 300, 40), "Press L to toggle light");
  }
}

function Update(){
  if (enableLight){ // if enableLight is true...
    if (Input.GetKeyDown("l")){ // and L pressed...
      pLight.light.enabled = !pLight.light.enabled; // toggle light
    }
  } else { // turn light off if enableLight is false
    pLight.light.enabled = false;
  }
}