positive and negative keys

Hey all, I am looking for a way to add two script functions on the same button “on” and “off” here I have so far…

function Update () {
if (Input.GetButton (“L”)) light.intensity = 0;};

I am looking to do another function with the same key strokew “L” to turn light conditions back to normal:

if (Input.GetButton (“L”) light.intensity = 0.5;);

what is the best way to do this without having the keystroke cancel the functions out? thanks - alex

I usually do toggle stuff like this –

var isOn : boolean = false;
function Update(){
	if (Input.GetButtonDown ("Light")){
		toggleLight();
	}
}

function toggleLight(){
	if(isOn){
		light.intensity = 0;
		isOn = false;
	}
	else{
		light.intensity = .5;
		isOn = true;
	}
}

If you don’t want to give it its own function,

var isOn : boolean = false;
function Update(){
	if (Input.GetButtonDown ("Light")){
		if(isOn){
			light.intensity = 0;
			isOn = false;
		}
		else{
			light.intensity = .5;
			isOn = true;
		}
	}
}

(using if/else, rather than two separate if statements, ensures only one of the two is executed)

I got this to work;

only problem is that the keystroke sensitivity is pretty high and it flashes between on and off in the game, any additional coding to help with this?

Oh, you’re using GetButton – that would return True every frame you hold the button. Do GetButtonDown to only do it when you first press it down.

thanks :slight_smile: