Search Unity

Pause not working

Discussion in 'Scripting' started by DaveyJJ, Jun 16, 2007.

  1. DaveyJJ

    DaveyJJ

    Joined:
    Mar 24, 2005
    Posts:
    1,558
    Code (csharp):
    1.  
    2. // to display a message in the Text Status GUIText object
    3. var DisplayGameStatus : GUIText;
    4. // determines if the game is paused by using a simple ON or OFF state set initially to OFF/0
    5. var AreWePaused : int = 0;
    6.  
    7.  
    8. // functions that are run when the game play levels are first loaded and run
    9. function Start () {
    10.     // when the level starts we hide the cursor and lock the cursor position to middle of screen
    11.     // remember to show and unlock cursor when level ends and score needs to be submitted
    12.     Time.timeScale = 1;
    13.     Screen.lockCursor = true;
    14.     Screen.showCursor = false;
    15. }
    16.  
    17. function Update () {
    18.     if (Input.GetButtonUp ("Pause")  AreWePaused == 0) {
    19.     Time.timeScale = 0;
    20.     AreWePaused = 1;
    21.     DisplayGameStatus.guiText.text = "Paused";
    22.     }
    23.    
    24.     if (Input.GetButtonUp ("Pause")  AreWePaused == 1) {
    25.     Time.timeScale = 1;
    26.     AreWePaused = 0;
    27.     DisplayGameStatus.guiText.text = "";
    28.     }
    29. }
    30.  
    This isn't working. I have defined an extra input called Pause and associated it with the letter "p". But when you hit "p" in game, nothing. This is attached to an empty game object in game

    Thoughts?
     
  2. hsparra

    hsparra

    Joined:
    Jul 12, 2005
    Posts:
    750
    When you "pause", you first change AreWePaused to 1, which works. Then you turn right around in the same frame where Input.GetButtonUp("Pause") is still true and check if AreWePaused is = 1, which is now is since it was just set to 1.

    How about
    Code (csharp):
    1.  
    2. // to display a message in the Text Status GUIText object
    3. var DisplayGameStatus : GUIText;
    4. // determines if the game is paused by using a simple ON or OFF state set initially to OFF/0
    5. var AreWePaused : int = 0;
    6.  
    7.  
    8. // functions that are run when the game play levels are first loaded and run
    9. function Start () {
    10.    // when the level starts we hide the cursor and lock the cursor position to middle of screen
    11.    // remember to show and unlock cursor when level ends and score needs to be submitted
    12.    Time.timeScale = 1;
    13.    Screen.lockCursor = true;
    14.    Screen.showCursor = false;
    15. }
    16.  
    17. function Update () {
    18.    if (Input.GetButtonUp ("Pause")) {
    19.         TogglePause();
    20.     }
    21. }
    22.  
    23. function TogglePause()
    24. {
    25.     if (AreWePaused == 0) {
    26.         Time.timeScale = 0;
    27.         AreWePaused = 1;
    28.         DisplayGameStatus.guiText.text = "Paused";
    29.      } else if (AreWePaused == 1) {    // Could just be an else
    30.         Time.timeScale = 1;
    31.         AreWePaused = 0;
    32.         DisplayGameStatus.guiText.text = "Going";
    33.     }
    34. }
    35.  
     
  3. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    If your DisplayGameStatus GUI only ever shows "Paused" (like in Unity Invaders/Unitroids), then you can leave the text as "Paused" and keep the object deactivated normally, and do this:

    Code (csharp):
    1.  
    2. function Update() {
    3.     if (Input.GetButtonDown("Pause") ) {
    4.         Time.timeScale = 1-Time.timeScale;
    5.         DisplayGameStatus.active = !DisplayGameStatus.active;
    6.     }
    7. }
    8.  
    That just toggles timeScale from 1 to 0 and vice versa, and toggles DisplayGameStatus from inactive to active and vice versa.

    I'd be more inclined to use GetButtonDown for this sort of thing, and if you'd rather use lfrog's method, I think it's better practice to use booleans for areWePaused type variables (true/false instead of 1/0). Not that it would function differently, but it makes the code more readable and potentially less prone to bugs.

    --Eric
     
  4. DaveyJJ

    DaveyJJ

    Joined:
    Mar 24, 2005
    Posts:
    1,558
    Thanks lfrog and Eric. This is what I'm after (it makes sense to a noob coder like me. I agree though that true/false is better than 1/0 but I only ever use 0 for OFF and 1 for ON so I rarely get confused.

    My problem thinking these sort of things through is that I mess up function Updates by always trying to throw in a yield statement (obviously not allowed) so forgetting that I can call a function within the function update to make things go. I must remember this.

    G'nite for now.