The question is answered, right answer was accepted
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");
}
}
Answer by tormentoarmagedoom · May 13, 2019 at 08:14 AM
Hello.
I think you are still learning code right ? :D 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... :D
thank you that worked :) 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)