- Home /
Pause: Issues with pausing, resuming, and pressing buttons within pause
It actually worked once, but now I can't get it to work at all. Either the music will still be running or when I press the button I set as resuming, nothing really happens. But when I am in the pause itself, my menu button will not work. I got this code by looking at a few other examples.
var click : AudioClip; var menuSkin : GUISkin; var areaWidth : float; var areaHeight : float; var theTexture : Texture2D;
static var pauseVisible : boolean; private var player : GameObject; private var cam : GameObject;
function Start() {
// initially hide the pause menu
pauseVisible = false;
player = GameObject.Find("First Person Controller");
cam = GameObject.Find("Camera");
}
function Update() {
if( Input.GetKeyUp( KeyCode.P ) )
{
Time.timeScale = 0;
pauseVisible = true;
player.GetComponent(SmoothFollow).enabled = false;
cam.GetComponent(SmoothFollow).enabled = false;
}
else if(Input.GetKeyUp(KeyCode.Space))
{
Time.timeScale = 1.0;
pauseVisible = false;
player.GetComponent(SmoothFollow).enabled = true;
cam.GetComponent(SmoothFollow).enabled = true;
}
}
function OnGUI () { GUI.skin = menuSkin;
if(pauseVisible)
{
GUI.color = Color.white;
GUI.color.a = 0.5;
GUI.DrawTexture(Rect(0, 0, Screen.width, Screen.height), theTexture);
var ScreenX = ((Screen.width * 0.5) - (areaWidth * 0.5));
var ScreenY = ((Screen.height * 0.5) - (areaHeight * 0.5));
GUILayout.BeginArea(Rect(ScreenX, ScreenY, areaWidth, areaHeight));
GUILayout.Box("Pause Menu");
if(GUILayout.Button("Main Menu"))
{
Application.LoadLevel(7);
}
if (GUILayout.Button("Quit"))
{
Application.Quit();
}
GUILayout.Label("Press space bar to resume!");
GUILayout.EndArea();
}
} @script RequireComponent (AudioSource)
Answer by sandolkakos · Jul 10, 2010 at 04:27 AM
when you use the Time.timeScale = 0, then all Functions stop all your actions, "except Function OnGUI".
then make this:
in Function Update, use this to call the Pause state:
if( Input.GetKeyUp( KeyCode.P ) )
{
Time.timeScale = 0;
pauseVisible = true;
player.GetComponent(SmoothFollow).enabled = false;
cam.GetComponent(SmoothFollow).enabled = false;
}
and in Function OnGUI, use this, to unPause state:
if(Input.GetKeyUp(KeyCode.Space)) {
Time.timeScale = 1.0;
pauseVisible = false;
player.GetComponent(SmoothFollow).enabled = true;
cam.GetComponent(SmoothFollow).enabled = true;
}
i am not an expert, but i think that this fix your problem ;)
No. Input functions, and Update() still runs when TimeScale is = 0. The only thing that changes is the Time (deltaTime, etc), as well as any rigidbodies (since they rely on time, as well). All input functions inside Update will still run, in fact, do not move the Input check to the OnGUI() function. You shouldn't ever check for input in the OnGUI() function unless absolutely necessary (and unless you know how to check for the correct event).
Answer by Ravi 1 · Nov 09, 2010 at 04:35 PM
Hello if you have not got the answer yet, please go through this post. I have made a package in order to solve this problem. It might help you.