- Home /
[4.6 GUI] Displaying my pause menu when ESC is pressed
EDITED QUESTION I have a script, in which i've layed out what i think would be the logical order of things that would need to happen when creating a pause menu. My question is, once the user presses ESC, how do I display the GUI Panel that i've already created using the new UI system. This Panel exists in a canvas which I made in another scene.
Thank you for any help! Even pointing me towards the right set of documentation. -Jon
using UnityEngine;
using System.Collections;
public class InGameGUI : MonoBehaviour {
private string GUImode = "gaming";
// Use this for initialization
void Start ()
{
}
// Update is called once per frame
void Update ()
{
if(Input.GetKeyDown("escape")) //Pause game when escape is pressed
{
Time.timeScale = 0;
GUImode = "paused";
}
}
public void DisplayMenu()
{
if(GUImode == "paused")
{
//display pause menu here
if(Input.GetKeyDown("escape")) //if escape is pressed to resume game
{
Time.timeScale = 1;
GUImode = "gaming";
}
if(/*add condition for when game is quit*/) //If quit button is pressed return to main menu
{
Time.timeScale = 1;
GUImode = "quitting";
Application.LoadLevel("MainMenu");
}
}
}
}
Answer by Kiwasi · Dec 06, 2014 at 08:55 PM
The UI system and OnGUI are completely separate systems, and should not be used together.
Move all of your OnGUI code to Update. Create a GameObject variable to store the panel. Use SetActive() to turn it on and off.
For the GameObject variable, can you give an example of how i would do that if the panel existed in another scene? Or is it a bad idea that my menu exists in another scene?
$$anonymous$$ake the panel a prefab by dragging out of the scene an into the assets folder. Then you can recreate it in your new scene. You can't access a GameObject in another scene without changing scenes.
AHHh yes! This is great. Thank you so much for your help!
Answer by Ouss · Dec 06, 2014 at 08:46 PM
You call GUI methods, like GUI.DrawTexture, GUI.BOX, etc... Here is a link to a thread having almost same matter: http://answers.unity3d.com/questions/623781/making-a-gui-panel-appear-on-boolean-true.html
I thought GUI methods were the old GUI way before 4.6 new GUI (maybe it's only referred to as UI??). Thank you for the link, i will be sure to check it out.