How i turn on/off a Object with GetKeyDown?

Hi guys, I’m trying to make a script so that when I v activate the “light” and when I click again to disable, but it’s not working :confused:

var onoff : boolean;

var luz : GameObject;

function Update(){

	if(Input.GetKeyDown ("v")){

		onoff = true;

	}

}

function ativar () {

    if (onoff == true)

        luz.active = true;

    if (onoff == false)

        luz.active = false;

}

Thanks.

You’re only changing onoff to true, never to false, but that was not making any difference at all, since the function ativar were not being called. You can toggle the onoff variable true and false with onoff = !onoff each time the key is pressed, and directly assign onoff to luz.active:

var onoff : boolean;
var luz : GameObject;

function Update(){

	if(Input.GetKeyDown ("v")){
		onoff = !onoff; // toggle the onoff variable
                luz.active = onoff;
	}
}

NOTE: if Unity complains that “active is obsolete”, use luz.enabled instead.