2 if statements being triggered at once

i have an if statement to turn on my torch and one to turn it off however it is triggering them both at the same time.

{

    if (Input.GetKeyDown(KeyCode.Q))
    {
        if (player.GetComponent<inventory>().hasTorch == true)
        {
            FlashLight.active = true;
            Debug.Log("toggleOn");
        }
    }

    if (Input.GetKeyDown(KeyCode.Q))
        if (FlashLight.active == true)
        {
            FlashLight.active = false;
            Debug.Log("ToggleOff");
        }
}

Hello.

I think you are still learning code right ? :smiley: This is a veru basic issue… Both if parts are true…

You have 2 if sentences, when code is executed, if first if is true, it will be executed, then it goes for the second if and if its true, its also executed.

You should debug your code while executing so you can understand why is this happening.

I supose you want this:

if (Input.GetKeyDown(KeyCode.Q))
     {
         if (player.GetComponent<inventory>().hasTorch == true)
         {
             if (FlashLight.active == true)
             {
             FlashLight.active = false;
             Debug.Log("ToggleOff");
             }
             else 
             {
             FlashLight.active = true;
             Debug.Log("toggleOn");
             }
         }
     }

Look that when ! is pressed, if have a torch, then have 2 options for execute (or light is active or not).

Bye!

PS: go spend 5 hours doing basic scripting tutorials… :smiley:

thank you that worked :slight_smile: yes i am still learning im studying game development but am currently on break for a few weeks so i wanted to just do some practice to keep my skills up however ive been battling that issue for days(i knew it would be an easy fix)