How to add two if statements to one result. (Flashlight being on, and pressing "F", to turn off the flashlight.)

I’m fairly new to unity, still getting the hang of it.

The issue I’m having is trying to figure out how to turn on and off the flashlight. What I came up with is this:

            public var flashLight: Transform;  

if(flashLight.enabled = true && Input.GetKeyDown("f"))

   flashLight.enabled = false;

You’re probably after something like this:

if(Input.GetKeyDown("f"))
{
   flashLight.enabled = !flashLight.enabled;
}

A Simpler Description for “THE IT” Answer. And in my personal view you should handle the GetKeyUp and not GetKeyDown.


if(Input.GetKeyUp(KeyCode.F))
{
      if(flashLight.enabled == true)
         flashLight.enabled = false;
      else
         flashLight.enabled = true;
}

public var flashLight: Transform;
if(Input.GetKeyDown(“f”)){
if(flashLight.enabled == true){
flashLight.enabled = false;
}
else(flashLight.enabled == false){
flashLight.enabled = true;
}
}