- Home /
Time.timeScale doesnt work with GUI?
Okay so I have a simple pause script and it has a resume button. If you pause(esc) the time.timeScale is 0 and all is well and if you hit escape again it goes back to 1 and all is still good. But for some reason the resume button doesnt work. If you hit resume, the menu will go away but the time will still be 0. using UnityEngine; using System.Collections;
public class PauseScript : MonoBehaviour {
//public GUISkin mySkin;
private Rect windowRect;
private bool paused = false;
private void Start(){
windowRect = new Rect (Screen.width / 2 - 100, Screen.height / 2 - 100, 200, 200);
}
void Update () {
if (Input.GetKeyDown(KeyCode.Escape)){
if (Time.timeScale == 1){
Time.timeScale = 0;
paused = true;
}
else
{
Time.timeScale = 1;
paused = false;
}
}
}
private void OnGUI(){
if (paused) {
windowRect = GUI.Window(0, windowRect, windowFunc, "Pause Menu");
}
}
private void windowFunc(int id){
if(GUILayout.Button("Resume")){
Time.timeScale = 1;
paused = false;
}
if(GUILayout.Button("Main menu")){
Application.LoadLevel(0);
Time.timeScale = 1;
}
if(GUILayout.Button("Reload Level")){
Application.LoadLevel(Application.loadedLevel);
Time.timeScale = 1;
}
if(GUILayout.Button("Quit")){
Application.Quit();
}
}
}
I ran your script and could not reproduce the problem. Can you reproduce the problem if you attach this script to an empty game object in a new scene?
A simpler way to accomplish pause is to move your pause code in a separate method. That way it behaves the same no matter where you call it from.
Unfortunately I can't find a problem in your script. If the pause menu disappears timeScale should be correct... Are you sure you aren't touching it elsewhere? Or maybe you have multiple instances of this component running around?
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Countdown after resume game. 4 Answers
Can't set Time.timeScale back to 1. 2 Answers
I made an resume button, but I don't know how to code it to close my pause menu, any tips? 2 Answers
Distribute terrain in zones 3 Answers