Flashlight activation issue

I wrote this script for my FPS, but when I press the “Flashlight” button (f), it turns off, but not on again. How to fix?

private bool FisOn = false;

void Update () {

if (Input.GetButtonDown ("Flashlight")) {
			FisOn = !FisOn; 
		}

		if (FisOn == true) {
			Flashlight.SetActive (true); 
		}

		if (FisOn == true) {
			Flashlight.SetActive (false); 
		}
}

Hey @connorwforman

The problem is the logic of your bool checks. Both of them are checking if FisOn is true, and the second one sets the flashlight to unactive so it will always be unactive (it overrides the first one since both checks use the exact same condition). If you want this to work you’ll need to change one of those checks to compare for a false value.

With that being said I have a couple of observations about your code.

First of all you should never do something like this bool check:

if(booleanVariable == true)
    //Do stuff

Why? Because it is redundant. The if conditional needs a boolean value and the variable already provides that. There is no need for you to compare your boolean variable with a true boolean constant. It is exaclty the same as multiplying by 1. Think about it, you are taking an extra step that outputs the exact same result if you hadn’t done it anyway.

The other point is that you could get rid of your last 2 bool checks and just do this inside your button down check.

flashlight.SetActive(FisOn);

And boom. No more boolchecks you just set it directly with the flipped bool value you already have. As you can see, when it comes to bools. less is more.

Hope this helps! :slight_smile: