- Home /
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;
Your main problem is that you assigned flashLight.enabled inside of your if condition ins$$anonymous$$d of comparing it to true.
= is the assignment operator
== is the compare operator.
Anyway to toggle a boolean @the-it posted the best solution.
Answer by The-IT664 · Apr 17, 2013 at 04:20 AM
You're probably after something like this:
if(Input.GetKeyDown("f"))
{
flashLight.enabled = !flashLight.enabled;
}
Answer by HmoudMO · Apr 17, 2013 at 09:54 AM
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;
}
Answer by jpc133 · Apr 17, 2013 at 12:53 PM
public var flashLight: Transform;
if(Input.GetKeyDown("f")){
if(flashLight.enabled == true){
flashLight.enabled = false;
}
else(flashLight.enabled == false){
flashLight.enabled = true;
}
}
Please format your code correctly.
This answer is not going to work.
In if(flashLight.enabled == true) you set it to false. Then in the very next conditional check if(flashLight.enabled == false) will set it back to true. It will work if you use else before your second conditional.
public var flashLight: Transform;
// in function
if(Input.Get$$anonymous$$eyDown("f")){
if(flashLight.enabled == true){
flashLight.enabled = false;
}
else if(flashLight.enabled == false){
flashLight.enabled = true;
}
}
Your answer
Follow this Question
Related Questions
Android: Switching Unity activity back to previous app 0 Answers
How To Switch Spatializer Plugin In The Runtime 0 Answers
Cipher using flashlights 1 Answer
Flashlight Flare 3 Answers
Flashlight Moving Awkwardly 0 Answers