Code (csharp): //Pause/Unpause Game if(Input.GetButtonDown("Q") gamePaused == false){ Time.timeScale = 0; gamePaused = true; } if(Input.GetButtonDown("Q") gamePaused == true){ Time.timeScale = 1; gamePaused = false; } Does nothing. gamePaused does not change. I've checked, and the input is set up correctly. Rganks.
What happens if you just do Code (csharp): if(Input.GetButtonDown("Q")){ Debug.Log("Q pressed"); } ? Maybe you can post your full script?
Presumably the first if condition is running, setting 'gamePaused' to true, and then the second if condition is running straight away. You could of course check this by adding two Debug.Log statements.
That is exactly what is happening. It is falling through the second if straightaway. Put an else in between them.
I have indeed noticed this problem, "" does not work most (if not all) of the time, so you need a second if statement. "Else" also won't help, it requires an alternate set of requirements to be filled (it basically says "or", not "and") so if{ if{ }} is how to do it.
Maybe you just need to change "GetButton..." to "GetKey..."... edit: oh, so I think this is not the problem. Good luck.
Explain, please. What I said is what I have observed as a coder, so it is based off of experience. Again, please explain.
In OP's case, the quickest fix is to put an else. He already has the "alternate set of requirements". "else" does not basically say "or"; It says "otherwise", or "Now for something completely different". Allow me to walk you through the OP's code Code (csharp): //Pause/Unpause Game //if the Q key was depressed this frame the gamePaused variable is false if(Input.GetButtonDown("Q") gamePaused == false){ //do our stuff: pause the game, and set the flag to true Time.timeScale = 0; gamePaused = true; } //if the Q key was depressed this frame the gamePaused variable is true //without the else, it will always flow into this if //if gamePaused were false, then the first if will change it true //if it were true, then it will not have gone in the first if, but still come through here if(Input.GetButtonDown("Q") gamePaused == true){ Time.timeScale = 1; gamePaused = false; } Now for an efficiency fix, which may look like Orbitus's suggest answer of an if inside an if, but it's not for the reasons of treating elses like ors and ands being faulty. Code (csharp): if(Input.GetButtonDown("Q")) { Time.timeScale = 0; if(gamePaused) { Time.timeScale = 1; } gamePaused = !gamePaused; } Cheers! Edit: Sorry noisecrime... great minds think alike and at the same time
Edit: Bah, Democre replied as I was typing out mine, basically saying the exact same thing. Then you have observed incorrectly. There is nothing wrong with '' it works perfectly fine. If you have code with a conditional statement using which doesn't work as expected then either you expectations/logic (as in the case of the OP) or code is wrong. As for the OP the problem is simple, if the first conditional check evaluates to true, then the second conditional must also evaluate to true due to the change of 'gamepaused' by the first, resulting in both if statements being executed, thus pausing then un-pausing the game in a single update. Hence why an else check is required. For example Assume that 'Input.GetButtonDown("Q")' is true and gamePaused is false. The first if statement will evaluate to true and will change gamePaused to true. Since 'Input.GetButtonDown("Q")' will not change its value half-way through Update it is still true for the second if statement and as the previous conditional check set gamePaused to true, the second if statement now also evaluates to being true. Its would be easy to check, just add a Debug.Log() statement to each conditional check and you'll see that is what is happening. Normally in these situations I would find it easier to split the check into a hierarchy chain e.g. Code (csharp): if(Input.GetButtonDown("Q") ) { if( gamePaused == false) { Time.timeScale = 0; gamePaused = true; } else { Time.timeScale = 1; gamePaused = false; } }
Sorry if you guys didn't understand, but at this post I had meant that I got it working. No more help needed. I was just letting Orbitus know that his logic was completely incorrect. Thanks everyone for explaining it to him.
Regardless of what you meant, the rest of your post was still incorrect. And for your information, else if works perfectly fine.
In programming, it is absolutely not the same. Please just stop. We are talking about scripting in a scripting forum. Else does not mean or in any case when you are talking about programming. I don't care if I'm supposed to be reading this as a human. What you wrote is absolutely incorrect when it comes to scripting.
Sorry, Democre, I was being the hater there... I changed the post. Seppi, else if may work, but the else isn't necessary is what I was trying to say, and I simply worded my post wrong, so please stop saying that I was incorrect, because it was just an honest mistake.
Sorry everyone, I did end up looking up "or" and I did remember that it is supposed to be used in "if ___ or ___ then _____" statements. It would have helped me a lot if you guys had simply told me to wait and explained this, because I would have remembered on the spot. I get flustered when people start attacking what I say, and when that happens I misinterpret information. The main reason I was confused with the or statements is because I don't use them, simply because I don't need them, so I forgot what they were used for. I'm sorry the situation had to go this far.