Search Unity

Game Not Pausing

Discussion in 'Scripting' started by Seppi, Nov 17, 2012.

  1. Seppi

    Seppi

    Joined:
    Nov 10, 2012
    Posts:
    162
    Code (csharp):
    1. //Pause/Unpause Game
    2.     if(Input.GetButtonDown("Q")  gamePaused == false){
    3.    
    4.         Time.timeScale = 0;
    5.         gamePaused = true;
    6.     }
    7.    
    8.     if(Input.GetButtonDown("Q")  gamePaused == true){
    9.    
    10.         Time.timeScale = 1;
    11.         gamePaused = false;
    12.     }
    Does nothing. gamePaused does not change. I've checked, and the input is set up correctly.

    Rganks.
     
  2. BlackMantis

    BlackMantis

    Joined:
    Feb 7, 2010
    Posts:
    1,475
    Try GetButtonUp
     
  3. Seppi

    Seppi

    Joined:
    Nov 10, 2012
    Posts:
    162
    For one, that wouldn't make a difference. It's also not what I want.
     
  4. Deleted User

    Deleted User

    Guest

    What happens if you just do

    Code (csharp):
    1.  
    2. if(Input.GetButtonDown("Q")){
    3.         Debug.Log("Q pressed");
    4. }
    5.  
    ?
    Maybe you can post your full script?
     
  5. Seppi

    Seppi

    Joined:
    Nov 10, 2012
    Posts:
    162
    That of course works fine. The issue here is that the time scale is not changing.
     
  6. Seppi

    Seppi

    Joined:
    Nov 10, 2012
    Posts:
    162
    So.....
     
  7. Zenix

    Zenix

    Joined:
    Nov 9, 2009
    Posts:
    213
    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.
     
  8. Democre

    Democre

    Joined:
    Mar 31, 2010
    Posts:
    345
    That is exactly what is happening. It is falling through the second if straightaway.
    Put an else in between them.
     
  9. Seppi

    Seppi

    Joined:
    Nov 10, 2012
    Posts:
    162
    Got it. Thanks a ton.
     
  10. OrbitusII

    OrbitusII

    Joined:
    Jul 4, 2011
    Posts:
    175
    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.
     
  11. Seppi

    Seppi

    Joined:
    Nov 10, 2012
    Posts:
    162
    Sorry to sound mean, but everything you said was wrong.
     
  12. Rodolfo-Rubens

    Rodolfo-Rubens

    Joined:
    Nov 17, 2012
    Posts:
    1,197
    Maybe you just need to change "GetButton..." to "GetKey..."...

    edit:
    oh, so I think this is not the problem. Good luck.
     
  13. OrbitusII

    OrbitusII

    Joined:
    Jul 4, 2011
    Posts:
    175
    Explain, please. What I said is what I have observed as a coder, so it is based off of experience. Again, please explain.
     
  14. Democre

    Democre

    Joined:
    Mar 31, 2010
    Posts:
    345
    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):
    1. //Pause/Unpause Game
    2. //if the Q key was depressed this frame  the gamePaused variable is false
    3.     if(Input.GetButtonDown("Q")  gamePaused == false){
    4.     //do our stuff: pause the game, and set the flag to true
    5.         Time.timeScale = 0;
    6.         gamePaused = true;
    7.     }
    8.  
    9.    
    10. //if the Q key was depressed this frame  the gamePaused variable is true
    11. //without the else, it will always flow into this if
    12. //if gamePaused were false, then the first if will change it true
    13. //if it were true, then it will not have gone in the first if, but still come through here
    14.     if(Input.GetButtonDown("Q")  gamePaused == true){
    15.         Time.timeScale = 1;
    16.         gamePaused = false;
    17.     }
    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):
    1. if(Input.GetButtonDown("Q")) {
    2.     Time.timeScale = 0;
    3.     if(gamePaused) {
    4.         Time.timeScale = 1;
    5.     }
    6.     gamePaused = !gamePaused;
    7. }
    Cheers!

    Edit: Sorry noisecrime... great minds think alike and at the same time
     
    Last edited: Nov 26, 2012
  15. Noisecrime

    Noisecrime

    Joined:
    Apr 7, 2010
    Posts:
    2,054
    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):
    1.  
    2.     if(Input.GetButtonDown("Q") )
    3.     {    
    4.         if( gamePaused == false)
    5.         {
    6.             Time.timeScale = 0;
    7.             gamePaused = true;
    8.         }
    9.         else
    10.         {
    11.             Time.timeScale = 1;
    12.             gamePaused = false;
    13.         }
    14.     }
    15.    
     
    Last edited: Nov 26, 2012
  16. Seppi

    Seppi

    Joined:
    Nov 10, 2012
    Posts:
    162
    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.
     
  17. OrbitusII

    OrbitusII

    Joined:
    Jul 4, 2011
    Posts:
    175
    I meant "else" as in "else if," people. My issue was forgetting a single word.
     
    Last edited: Nov 26, 2012
  18. Democre

    Democre

    Joined:
    Mar 31, 2010
    Posts:
    345
    Not hatin', else never means or, with or without an if.
     
  19. OrbitusII

    OrbitusII

    Joined:
    Jul 4, 2011
    Posts:
    175
    Compound meanings. "Otherwise if _______" is the same as "or ________".
     
  20. Seppi

    Seppi

    Joined:
    Nov 10, 2012
    Posts:
    162

    Regardless of what you meant, the rest of your post was still incorrect. And for your information, else if works perfectly fine.
     
  21. Democre

    Democre

    Joined:
    Mar 31, 2010
    Posts:
    345
    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.
     
  22. OrbitusII

    OrbitusII

    Joined:
    Jul 4, 2011
    Posts:
    175
    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.
     
  23. OrbitusII

    OrbitusII

    Joined:
    Jul 4, 2011
    Posts:
    175
    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.
     
  24. rame16

    rame16

    Joined:
    Nov 21, 2012
    Posts:
    29
    It works for me! Thank you.