Time is still frozen whenever I reset my level after the countdown timer runs out

Hey everyone,

Let me give a little context for what I’m trying to do with this game. This is a small game where you have to complete the level within 30 seconds. When the countdown timer reaches 0, I have a restart screen that pops up and pauses the game. There’s a button on this screen that gives you the option to restart the level. However, when I hit the restart button, it restarts the level but with the time still frozen.

  • Sidenote: Whenever I’m in this spot where I just hit the restart button but time is still frozen, I can pause and unpause the game, and time will start working normally again.

I’m using this same script so the restart screen will pop up whenever I die from hitting spikes or an enemy, however everything works perfectly when this happens for some reason. I’ll die, the restart screen will pop up, I hit the restart button, then the level restarts like it should with time unfrozen.

Can anyone help me figure out what is going on here? This is my first real attempt at making a game solo and I’m still pretty much a newbie to programming. I’ll post my “DeathMenu” and “CountdownTimer” scripts below. If there is any other info anyone needs to answer my question better, just let me know. Thanks!

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class DeathMenu : MonoBehaviour
{
    public static bool gameIsPaused = false;

    public GameObject deathMenuUI;

    public void RestartLevel()
    {
        Time.timeScale = 1f;
        gameIsPaused = false;
        SceneManager.LoadScene(SceneManager.GetActiveScene().name);
    }

    public void DeathMenuPopUp()
    {
        deathMenuUI.SetActive(true);
        Time.timeScale = 0f;
        gameIsPaused = true;
    }

    public void LoadMenu()
    {
        Time.timeScale = 1f;
        SceneManager.LoadScene("Start Menu");
    }

    public void QuitGame()
    {
        Debug.Log("Quitting game...");
        Application.Quit();
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro;

public class CountdownTimer : MonoBehaviour
{
    float currentTime = 0;
    float startingTime = 5;

    [SerializeField] TMP_Text countdownText;

    void Start()
    {
        currentTime = startingTime;
    }

    void Update()
    {
        currentTime -= 1 * Time.deltaTime;
        countdownText.text = currentTime.ToString ("0");

        if (currentTime <= 0)
        {
            currentTime = 0;
        }

        if (currentTime == 0)
        {
            FindObjectOfType<DeathMenu>().DeathMenuPopUp();
        }
    }
}

First check is to make sure something is executing the line where it sets Time.timeScale back to 1.0.

Second check is make sure something else isn’t setting it back to zero!

You must find a way to get the information you need in order to reason about what the problem is.

What is often happening in these cases is one of the following:

  • the code you think is executing is not actually executing at all
  • the code is executing far EARLIER or LATER than you think
  • the code is executing far LESS OFTEN than you think
  • the code is executing far MORE OFTEN than you think
  • the code is executing on another GameObject than you think it is
  • you’re getting an error or warning and you haven’t noticed it in the console window

To help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

Doing this should help you answer these types of questions:

  • is this code even running? which parts are running? how often does it run? what order does it run in?
  • what are the values of the variables involved? Are they initialized? Are the values reasonable?
  • are you meeting ALL the requirements to receive callbacks such as triggers / colliders (review the documentation)

Knowing this information will help you reason about the behavior you are seeing.

If your problem would benefit from in-scene or in-game visualization, Debug.DrawRay() or Debug.DrawLine() can help you visualize things like rays (used in raycasting) or distances.

You can also call Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene manually, looking for all the parts, where they are, what scripts are on them, etc.

You can also call GameObject.CreatePrimitive() to emplace debug-marker-ish objects in the scene at runtime.

You could also just display various important quantities in UI Text elements to watch them change as you play the game.

If you are running a mobile device you can also view the console output. Google for how on your particular mobile target, such as this answer or iOS: How To - Capturing Device Logs on iOS or this answer for Android: How To - Capturing Device Logs on Android

Another useful approach is to temporarily strip out everything besides what is necessary to prove your issue. This can simplify and isolate compounding effects of other items in your scene or prefab.

Here’s an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:

1 Like

My assumption is that the CountdownTimer calls FindObjectOfType().DeathMenuPopUp(); after you restart the level. Which sets the timescale to 0.

There might be something else since i cant see where you close the UI.

1 Like

This was it! Thank you very much for the detailed reply, the rest of this info will help me a lot in the future I’m sure. The problem was that the if statement that activated the Death Menu in the CountdownTimer script was running every frame, which I believe was causing the time scale to be set back to 0 over and over. I will list my solution below. I used Debug.Log to figure out this was being called several hundred times after the time ran out.

if (currentTime == 0 && timeIsUp == false)
        {
            Debug.Log("Time has reached 0");
            timeIsUp = true;

            if (timeIsUp)
            {
                FindObjectOfType<DeathMenu>().DeathMenuPopUp();
            }
        }

I having the same issue