Unpause the game while the countdown timer goes off

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

Your timer doesn’t work because you called function on button click so it will call only when you press the escape key, I modified your code so try this one. Hope you want this way

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

public float seconds = 5;

public float minutes = 0;

void Update()

{

// If user hit escape button

if (Input.GetKeyDown(“escape”))

{

// Set the paused to togglePause method

paused = ! paused;

//if pause false then reset your timer
if( !paused )
{
seconds = 5;
minutes = 0;

//Reset your time scale
Time.timeScale = 1;
}

}

if( 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);

}

}

}

Thank you sir ajaykewat. :smile: it works