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?

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!

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;

}