- Home /
GUIText buttons in my pause menu don't fulfill their function until I unpause?
I have this script to pause my game when Escape is pressed
var pause : boolean = false;
var pauseGUI : GUIText;
var exitGUI : GUIText;
var menuGui : GUIText;
pauseGUI.enabled = false;
exitGUI.enabled = false;
menuGui.enabled = false;
function Update()
{
if(Input.GetKeyUp(KeyCode.Escape))
{
if(pause==true)
{
pause = false;
audio.Play();
}
else
{
pause = true;
audio.Stop();
}
if(pause == true)
{
Time.timeScale = 0.0;
pauseGUI.enabled = true;
exitGUI.enabled = true;
menuGui.enabled = true;
}
else
{
Time.timeScale = 1.0;
pauseGUI.enabled = false;
exitGUI.enabled = false;
menuGui.enabled = false;
}
}
}
Once escape is pressed, a couple of GUITexts appear, two of which are Exit, which closes the game, and Main Menu, which loads the scene that is my Main Menu. These use this script
var levelToLoad : String;
var soundhover : AudioClip;
var beep : AudioClip;
var QuitButton : boolean = false;
function OnMouseEnter()
{
audio.PlayOneShot(soundhover);
}
function OnMouseUp()
{
audio.PlayOneShot(beep);
yield new WaitForSeconds(0.35);
if(QuitButton)
{
Application.Quit();
}
else
{
Application.LoadLevel(levelToLoad);
}
}
@script RequireComponent(AudioSource)
What happens is when I pause the game, and click the main menu button, it doesn't work until I unpause the game, then it loads the main menu. If I build the game, the same thing happens with the Exit button. Any ideas on how to make the buttons work immediately, instead of unpausing first?
Answer by caleb_b · Apr 22, 2014 at 04:07 AM
After a bunch of views, but no answers to my question, I was able to find a new way of making my pause menu. I got this script from another Unity question, and it creates the pause menu by using GUI buttons, instead of my way of using GUITexts as my buttons. Here is the script, which I was able to copy and paste, and it worked with no errors.
using UnityEngine;
using System.Collections;
public class Menu : MonoBehaviour
{
public Rect windowRect = new Rect(295,175,0,0);
public bool gamePaused = false;
void Update()
{
if(Input.GetKeyUp(KeyCode.Escape))
{
if(gamePaused)
{
gamePaused = false;
Time.timeScale = 1.0f;
} else
{
gamePaused = true;
Time.timeScale = 0.0f;}
}
}
void OnGUI()
{
if (gamePaused)
{
Time.timeScale = 0.0f;
GUILayout.Window(0, windowRect, PauseMenu,"Game Paused ", GUILayout.Width(100));
}
}
void PauseMenu(int windowPause)
{
if (GUILayout.Button("Resume"))
{
Time.timeScale = 1.0f;
gamePaused = false;
}
if (GUILayout.Button("Exit"))
{
Application.Quit ();
}
}
}
This works great for me now. Plus, buttons can be easily added.
Your answer
![](https://koobas.hobune.stream/wayback/20220613142820im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
using playerprefs to update a guitext and record highscores 1 Answer
DontDestroyOnLoad duplication. 0 Answers
2 int's displayed in one GUIText. 3 Answers
how do i attach GUITEXT to a prefab 1 Answer