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

[4.6 GUI] Displaying my pause menu when ESC is pressed

Discussion in 'UGUI & TextMesh Pro' started by agentc0re, Dec 6, 2014.

  1. agentc0re

    agentc0re

    Joined:
    Feb 28, 2014
    Posts:
    77
    I am in need of some help or at least a point in the right direction.
    I am trying to figure out how to display the pause menu i made with the new 4.6 UI by calling it in a script. My goal here is that, when ESC is pressed, a state change is made and then i need to display my pause menu.
    Here's the script i'd be placing that code in.
    I am following a tutorial that uses the old GUI system. I believe that one problem i would run into is the function named OnGUI(). That's probably a unity function isn't it?
    Also it's probably worth noting, that i currently made the pause menu in a separate scene than the one that i'd be calling it in. I'm sure this matters but does it make what i'm trying to accomplish impossible?

    Thank you in advanced for any help!
    -Jon

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class InGameGUI : MonoBehaviour {
    5.  
    6.     private string GUImode = "gaming";
    7.  
    8.     // Use this for initialization
    9.     void Start ()
    10.     {
    11.    
    12.     }
    13.    
    14.     // Update is called once per frame
    15.     void Update ()
    16.     {
    17.         if(Input.GetKeyDown("escape")) //Pause game when escape is pressed
    18.         {
    19.             Time.timeScale = 0;
    20.             GUImode = "paused";
    21.         }
    22.     }
    23.  
    24.     public void OnGUI()
    25.     {
    26.         if(GUImode == "paused")
    27.         {
    28.             //display pause menu here
    29.  
    30.             if(Input.GetKeyDown("escape")) //if escape is pressed to resume game
    31.             {              
    32.                 Time.timeScale = 1;
    33.                 GUImode = "gaming";
    34.             }
    35.             if(/*add condition for when game is quit*/) //If quit button is pressed return to main menu
    36.             {
    37.                 Time.timeScale = 1;
    38.                 GUImode = "quitting";
    39.                 Application.LoadLevel("MainMenu");
    40.             }
    41.         }
    42.     }
    43. }
    44.  
     
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,398
    You can activate and deactivate the canvas your UI is on, using GameObject.SetActive. I wouldn't recommend having the pause menu be in a separate scene since that seriously complicates things.

    --Eric
     
  3. agentc0re

    agentc0re

    Joined:
    Feb 28, 2014
    Posts:
    77
    Thank you so much. This route worked great for me. As i am sure you can tell, i'm still learning here so thank you for taking the time to probably tell me something really fundamental.
    -Jon