- Home /
Dumb question :D
Else and If
This is probably a stupid question, but I can't seem to understand it. I am at an intermediate level of programming and I've used if many times before, but this one confuses me. If I have a conditional statement:
if (Input.GetKey(KeyCode.Escape /* Example*/) && condition1 && condition2) {
whatever = true;
} else {
whatever = false;
}
Even though my conditions are not false and escape has been let up, my boolean 'whatever' is set to false. I don't understand how the conditions can remain true and 'whatever' is set to false. Can anyone explain?
This code should definitely set whatever to true. Could you post your actual code?
It does set whatever true. I understand now. That's what happens when you question yourself :)
Answer by CodeMasterMike · Nov 22, 2012 at 06:24 AM
The Input.GetKey only returns true when its pressed down, so in your case the whatever variable is set to false when the Escape key is not pressed down.
Here is the documentation for GetKey: http://docs.unity3d.com/Documentation/ScriptReference/Input.GetKey.html
Good luck!
Answer by DeveshPandey · Nov 22, 2012 at 07:30 AM
I'm describing flow of code, have a look-
- when you pressed a 'Esc' key then 'whatever' goes true
- when you release 'Esc' key then 'whatever' goes false, because Input.GetKey(KeyCode.Escape) returns false.
I don't know what you want to do but try this, may be help you:
if ((Input.GetKey(KeyCode.Escape / Example/) && condition1 && condition2) || !whatever) {
whatever = true;
}
else {
whatever = false;
}
I don't fully understand the purpose of this code. If whatever is false, it gets set to true. If whatever is true, it gets set to false, unless the escape key is pressed, in which case it stays true.
That would make whatever remain true while the game is running.
Follow this Question
Related Questions
Problem with On/Off Switch 1 Answer
If...else statement with errors 1 Answer
Use of boolean functions in c# 1 Answer
Better way to delay a function for a few seconds? Javascript 1 Answer
Button if else statement? 1 Answer