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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

A pause menu using a button and click.

Discussion in 'Editor & General Support' started by SeanSabe, Jun 6, 2015.

  1. SeanSabe

    SeanSabe

    Joined:
    Jan 20, 2015
    Posts:
    3
    I have a button and I want to use it for pause and unpause the menu when I click on it. ¿How I can do it? I appreciate your help.
     
    Last edited: Jun 6, 2015
  2. TonanBora

    TonanBora

    Joined:
    Feb 4, 2013
    Posts:
    493
    Hey there SeanSabe!
    If you just want to pause your game, you will want to set the time scale too zero.
    To do this, you need to make a script, and somewhere in the script create a public method with this bit of code in it (C#):

    Code (CSharp):
    1. Time.timescale = 0f
    Once this is done, you will need to call the method when you click the button.
    I would suggest you watch this video to learn how to do this if you do not know already:

    http://unity3d.com/learn/tutorials/modules/beginner/ui/ui-button

    Of course, you will also wish to unpause your game too. :p
    So I suggest creating a float variable that you store the starting timescale in before you set the scale to zero.
    Once you have the original timescale safely stored, you then call the Time.timescale function above, but instead of setting it too zero, you set it to the variable holding the starting timescale.


    Hope this helps! :)
    ~TB
     
  3. SeanSabe

    SeanSabe

    Joined:
    Jan 20, 2015
    Posts:
    3
    Thanks for the answer. Actually that's what I did. It might not be pretty efficient but it does what I need.

    Code (CSharp):
    1.  
    2.  void Start () {
    3.         Canvas_PauseMenu = Canvas_PauseMenu.GetComponent<Canvas> ();
    4.         Button_Pause = Button_Pause.GetComponent<Button> ();
    5.         Button_Resume = Button_Resume.GetComponent<Button> ();
    6.         Canvas_PauseMenu.enabled = false;
    7.         Button_Resume.enabled = false;
    8.         Button_Resume.gameObject.SetActive (false);
    9.     }
    10.    
    11.     // Update is called once per frame
    12.     public void PauseTest () {
    13.  
    14.         if(!active){
    15.             PauseGame();
    16.         }
    17.         else{
    18.             ResumeGame();
    19.         }
    20.  
    21.     }
    22.  
    23.     public void BackToMainMenu()
    24.     {
    25.         Application.LoadLevel (0);
    26.     }
    27.  
    28.     public void PauseGame()
    29.     {
    30.         Canvas_PauseMenu.enabled = true;
    31.         Button_Exit.enabled = true;
    32.         Button_Pause.enabled = false;
    33.         Button_Pause.gameObject.SetActive (false);
    34.         Button_Resume.enabled = true;
    35.         Button_Resume.gameObject.SetActive (true);
    36.         active = true;
    37.         Time.timeScale = 0;
    38.     }
    39.  
    40.     public void ResumeGame()
    41.     {
    42.         Canvas_PauseMenu.enabled = false;
    43.         Button_Exit.enabled = false;
    44.         Button_Pause.enabled = true;
    45.         Button_Pause.gameObject.SetActive (true);
    46.         Button_Resume.enabled = false;
    47.         Button_Resume.gameObject.SetActive (false);
    48.         active = false;
    49.         Time.timeScale = 1;
    50.     }
    51.  
    52.  
    53.