turn me on, turn me of

I did this script to turn a traget, a light perhaps on/off

var target : GameObject;

function Start () {
   target.light.enabled = true;
   }

function Update () {
   if (Input.GetKeyDown ("t")){
      target.light.enabled = false;
   }
   if (Input.GetKeyDown ("g")){
      target.light.enabled = true;
   }     
}

this way i need 2 Buttons, Is there a way to cycle it , by pressing the same one button again?

Like this?

function Update () {
   if (Input.GetKeyDown ("t")){
      target.light.enabled = 1-target.light.enabled;
   }
}

EDIT: Removed unnecessary timer reference.

Or if you don’t want to bring maths into a boolean operation:

function Update () {
   if (Input.GetKeyDown ("t")){
      target.light.enabled = !target.light.enabled;
   }
}

Despite what the name might suggest, GetKeyDown only triggers when a key is first pressed, so you don’t need to mess with extra code to prevent multiple changes per press.

Given the OP, I thought the numeric version might be easier to understand. I’ve removed the reference to timing. Thanks for the correction.

thanks alot ! :smile: