Problem setting Time.timeScale to 1 after being set to 0
So I've just made a script that will pause the game and bring up a window with three buttons (main menu, retry and continue) once the player has completed the level or failed it. This is what it looks like, except when the player fails (run out of time), it says "You ran out of time!". Apart from that there is no other difference. All the buttons function the same regardless.
When the player completes the level or fails it, Time.timeScale is set to 0. This is when the window comes up and the player can click on one of the buttons (what they do is self-explanatory). Now if the player completes the level and then chooses one of these options, Time.timeScale is set to 1 and will load the scene, and everything works perfectly. However, if the player runs out of time and fails, when the player chooses to go to the Main Menu, retry or continue, Time.timeScale doesn't seem to set back to 1 and the game is still paused but the correct scene is loaded so I know the function is being called. I know it's something to do with the timer as I've called the same Fail function I created using OnCollisionEnter (so when the player collided with a certain object they fail), and everything worked as it should. I'm not a programmer so maybe the answer is completely obvious but here is the script:
private int levelNumber;
public Canvas endLevelCanvas;
public Text outcomeText;
public Text timerText;
public float startTime;
private string currentTime;
// Use this for initialization
void Start () {
levelNumber = Application.loadedLevel;
endLevelCanvas.enabled = false;
}
// Update is called once per frame
void Update () {
startTime -= Time.deltaTime;
currentTime = string.Format ("{0:0.0}", startTime);
timerText.text = currentTime;
if (startTime <= 5) {
timerText.color = Color.red;
}
if (startTime <= 0) {
FailLevel ();
}
if (Input.GetKey (KeyCode.Escape)) {
QuitToMenu ();
}
}
public void FailLevel () {
Time.timeScale = 0;
endLevelCanvas.enabled = true;
outcomeText.text = "You ran out of time!";
}
public void WinLevel () {
Time.timeScale = 0;
endLevelCanvas.enabled = true;
outcomeText.text = "Level Completed!";
}
public void QuitToMenu () {
Application.LoadLevel (0);
Time.timeScale = 1;
}
public void ContinueToNextLevel () {
Time.timeScale = 1;
Application.LoadLevel (levelNumber + 1);
}
public void RetryLevel () {
Time.timeScale = 1;
Application.LoadLevel (levelNumber);
}
}
Sorry if I've missed something, I'm new to Unity Answers. Any help is greatly appreciated, thanks!
Only thing I'd be worried about is QuitTo$$anonymous$$enu, I would reorder the execution of timeScale and LoadLevel. Other than that, add some debugs to make sure you are calling these method and not some other method to load your new level.
Ah I forgot to change that one, originally they all executed LoadLevel first but I changed them around to see if it did anything, unfortunately that wasn't it :( I've already tried adding debugs to make sure they were working and it appeared they were still being called so I'm not too sure what's going on there. Thanks for the reply anyway!
Does it happen for every level load? or just a specific level, find all places where you change timeScale and put a debug before, maybe something else is being called on the start of the new level?
This may be a bug, people had a similar issue here, though it was quite a while ago. I would suggest either delaying the LoadLevel call by 1 frame so the timeScale is properly reset first, or alternatively, set the timeScale to 1 on the start or awake of each level ins$$anonymous$$d.
Every scene that can be loaded from that window (main menu scene, current scene and the next scene) have this problem (only when the player fails though. If the player completes it, everything works fine). Thanks for the link, I'll try delaying or setting the timeScale at the start of each level, see how I go. Thanks a lot, appreciate it!