Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Toggle Pause Button help :)

Discussion in 'Scripting' started by StarShipMan, Oct 10, 2014.

  1. StarShipMan

    StarShipMan

    Joined:
    Jan 28, 2014
    Posts:
    91
    Hi,

    I struggling to create a toggle for a pause button on screen. I am able to pause the game when i first press the pause button, however when i try to un pause the game by pressing the pause button again nothing happens . I tried adding a delay to my pauseInt variable so it can get called a second later however this did not help either.

    Please assist.

    Code (CSharp):
    1.     void OnMouseDown()
    2.     {
    3.         Debug.Log ("The Mouse is down" + this.name);
    4.         guiTex1.enabled = false;
    5.         guiTex2.enabled = true;
    6.  
    7.         if(pauseInt == 0)
    8.         {
    9.             Debug.Log ("Pause Button enabled");
    10.             gameController.PauseTimer();
    11.             Invoke("DelayPauseButtonReset", 0.5f);
    12.         }
    13.  
    14.         if(pauseInt == +1)
    15.         {
    16.             Debug.Log ("Pause Buton disabled");
    17.             gameController.StartTimer();
    18.             pauseInt = 0;
    19.         }
    20.     }
    21.    
    22.     void OnMouseUp()
    23.     {
    24.         guiTex2.enabled = false;
    25.         guiTex1.enabled = true;
    26.         Debug.Log ("The Mouse is up" + this.name);
    27.     }
    28.  
    29.     void DelayPauseButtonReset()
    30.     {
    31.         pauseInt = +1;
    32.         Debug.Log ("DelayPauseButtonReset has been enabled");
    33.     }
    34. }
     
  2. Pirs01

    Pirs01

    Joined:
    Sep 30, 2012
    Posts:
    389
    What's going on here:
    Code (CSharp):
    1. if(pauseInt == +1)
    Why the + operator? I'm supprised it even compiles.

    Not sure why you though delaying would help.
    You should never use Invoke. It's unsafe and very slow.
    For delaying things look into coroutines.
     
  3. Pirs01

    Pirs01

    Joined:
    Sep 30, 2012
    Posts:
    389
    Looked at the doc for + operator: http://msdn.microsoft.com/en-us/library/k1a63xkz.aspx and turns out this is correct: "Unary + operators are predefined for all numeric types. The result of a unary + operation on a numeric type is just the value of the operand."

    EDIT: OK I see why the dealy. You check for the pasuInt == 0 then set it to 1 and then checking it to be 1 right after. Use if else instead of 2 ifs

    I don't see problems otherwise but I'm not sure I understand the whole "Pause Button enabled" concept. Can you provide broader context to this code?

    EDIT: It looks overcomplex. Can't you just do:
    Code (csharp):
    1. if (GUI.Button(Rect(10,10,50,50), "Pause")
    2. {
    3.   if (bPaused)
    4.     gameController.StartTimer();
    5.   else
    6.     gameController.PauseTimer();
    7.  
    8.   bPaused = !bPaused;
    9. }
     
    Last edited: Oct 10, 2014
  4. StarShipMan

    StarShipMan

    Joined:
    Jan 28, 2014
    Posts:
    91
    Hi Pirs01,

    Thank you for the detailed reply and suggestions, ive been level designing for the last 4 months so didn't do any programming during this time, so im a bit rusty :)

    I managed to fix the toggle script. This is the updated version if anyone else is following this thread.

    Code (CSharp):
    1. void Start()
    2.     {
    3.         gameControllerObject = GameObject.Find ("GameManager");
    4.        
    5.         if (gameControllerObject != null)
    6.         {
    7.             gameController = gameControllerObject.GetComponent<GameController> ();
    8.         }
    9.         else
    10.             Debug.Log ("Cant find any 'GameController' scripts in Pause Button");
    11.     }
    12.    
    13.    
    14.     void OnMouseDown()
    15.     {
    16.         Debug.Log ("The Mouse is down" + this.name);
    17.         guiTex1.enabled = false;
    18.         guiTex2.enabled = true;
    19.  
    20.         if(!PauseGame)
    21.         {
    22.             gameController.StartTimer();
    23.             Debug.Log ("Game has been PAUSED");
    24.             PauseGame = true;
    25.         }
    26.         else
    27.         {
    28.             gameController.PauseTimer();
    29.             Debug.Log ("Game has been UNPAUSED");
    30.             PauseGame = false;
    31.         }
    32.     }
    33.    
    34.     void OnMouseUp()
    35.     {
    36.         guiTex2.enabled = false;
    37.         guiTex1.enabled = true;
    38.         Debug.Log ("The Mouse is up" + this.name);
    39.     }
    40. }