I already made the game to pause and unpause by clicking escape button. Right now I want to when the game is paused and user clicking escape button again, the count down timer is ticking, and when the count down timer show the time reach 0, the game will be unpause. The problem is, when user clicking escape button again when the game is paused, the count down timer is not ticking and the game unpaused immediately.
Here is the code so far:
using UnityEngine;
using System.Collections;
// Pause Logic
public class Pause : MonoBehaviour
{
public static bool paused = false; // For checking whether the game is paused or not
float seconds = 5;
float minutes = 0;
void Update()
{
// If user hit escape button
if (Input.GetKeyDown("escape"))
{
// Set the paused to togglePause method
paused = TogglePause();
}
}
void OnGUI()
{
// If paused is true
if (paused)
{
// Show the Paused GUI Box
GUI.Box(new Rect(Screen.width / 3 - 5, Screen.height - 30, 450, 25), "Paused, Press Escape button again to unpause");
}
}
bool TogglePause()
{
// If the time scale is 0 or pause
if (Time.timeScale == 0.0f)
{
// Set time scale to 1
Time.timeScale = 1f;
// and set to the false
return (false);
}
// If the time scale is 1 or unpause
else
{
if (seconds <= 0)
{
// Set time scale to 0
Time.timeScale = 0.0f;
if (minutes >= 1)
{
minutes--;
}
else
{
minutes = 0;
seconds = 0;
GameObject.Find("Pause").guiText.text = minutes.ToString("f0") + ":0" + seconds.ToString("f0");
}
}
else
{
seconds -= Time.deltaTime;
}
if (Mathf.Round(seconds) <= 9)
{
GameObject.Find("Pause").guiText.text = minutes.ToString("f0") + ":0" + seconds.ToString("f0");
}
else
{
GameObject.Find("Pause").guiText.text = minutes.ToString("f0") + ":" + seconds.ToString("f0");
}
return (true);
}
}
}
Your answer much appreciated!
Thank you