Search Unity

pause menu using unity GUI

Discussion in 'Immediate Mode GUI (IMGUI)' started by darthevil, Sep 6, 2010.

  1. darthevil

    darthevil

    Joined:
    Sep 6, 2010
    Posts:
    13
    I'm trying to make a pause menu in my game and there are a few things I'm not sure how to do. I've seen other posts about pause menu's but none of them have really been any help.

    First of all I have made a button which loads up a screen in the game for my menu but when the menu loads all of my in-game GUI features are still in the background. Does anyone know how to remove them and then bring them back when the menu is closed?

    Also, I need the game to pause when the menu opens up. How would I do this?

    For anyone offering advice, I'm new to scripting so try to keep it as simple as possible :D
     
  2. Jesse Anders

    Jesse Anders

    Joined:
    Apr 5, 2008
    Posts:
    2,857
    By 'in-game GUI' features, are you talking about stuff like health bars, score, etc.? If so, not displaying them when the menu is open is as simple as not displaying them when the menu is open. (Remember, Unity's GUI system - which I assume is what you're using - is immediate mode.)

    In other words, you most likely just need to wrap all of your in-game GUI code like this:

    Code (csharp):
    1. if (!menuIsOpen) {
    2.     // Do in-game GUI stuff.
    3. }
    To pause the physics simulation, you can set Time.timeScale to 0. Other components can be paused by disabling them ('enabled = false'). The way I handle this is to derive all components that I want to be 'pausable' from a Pausable class that inherits from Monobehaviour (this is in C#). Then, when the game needs to be paused or unpaused, I find all objects of type Pausable (and also ParticleEmitter) using FindObjectsOfType(), and enable or disable as needed. (FindObjectsOfType() is said to be quite slow; I don't know how many objects you'd have to have in the scene in order for it to be a problem in this context, but it's something to be aware of.)
     
  3. TheCowMan

    TheCowMan

    Joined:
    Aug 24, 2010
    Posts:
    387

    a simple pause script i threw together.
    Code (csharp):
    1.  
    2.  
    3. function Start(){
    4. IsPaused = false;
    5.  
    6. }
    7.  
    8. function Update () {
    9. if (Input.GetKeyDown(KeyCode.T) ){
    10.     IsPaused = true;
    11.     GetComponent(MouseLook).enabled = false;
    12.     }
    13. }
    14.  
    15. function OnGUI(){
    16.     if (IsPaused){
    17.     GUI.Box (Rect (580,20,100,210), "Teleport");
    18.    
    19.     // Make the Quit button.
    20.     if (GUI.Button (Rect (590,160,80,20), "Quit")) {
    21.         Application.Quit;
    22.     }
    23.    
    24.     if(GUI.Button (Rect(590,190,80,35), "Back")){
    25.         IsPaused = false;
    26.         GetComponent(MouseLook).enabled = true;
    27.        
    28.     }
    29. }
    30.  
    31. }
    32.  
     
  4. darthevil

    darthevil

    Joined:
    Sep 6, 2010
    Posts:
    13
    Thanks for your comments thats been really helpful :wink:
     
  5. Brushy

    Brushy

    Joined:
    Mar 28, 2013
    Posts:
    1
    it says ispause doesnt exist
     
  6. andrewjm10

    andrewjm10

    Joined:
    Jun 9, 2013
    Posts:
    1
    @ Brushy, You have to add
    var IsPaused;

    Then fix:
    Application.Quit;
    to
    Application.Quit();

    ^ It will throw more errors, i just tried it works fine, except for the damn mouse look Up/Down still on lol.
     
  7. epichi123

    epichi123

    Joined:
    Feb 15, 2014
    Posts:
    16
    It still says:
    Unknown identifier 'IsPaused':confused:
     
  8. Artermis

    Artermis

    Joined:
    Feb 11, 2018
    Posts:
    5
    IsPaused should be isPaused.