Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

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

Discussion in 'Scripting' started by jigginsrult, Jun 5, 2022.

  1. jigginsrult

    jigginsrult

    Joined:
    Apr 15, 2021
    Posts:
    2
    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!

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.SceneManagement;
    5.  
    6. public class DeathMenu : MonoBehaviour
    7. {
    8.     public static bool gameIsPaused = false;
    9.  
    10.     public GameObject deathMenuUI;
    11.  
    12.     public void RestartLevel()
    13.     {
    14.         Time.timeScale = 1f;
    15.         gameIsPaused = false;
    16.         SceneManager.LoadScene(SceneManager.GetActiveScene().name);
    17.     }
    18.  
    19.     public void DeathMenuPopUp()
    20.     {
    21.         deathMenuUI.SetActive(true);
    22.         Time.timeScale = 0f;
    23.         gameIsPaused = true;
    24.     }
    25.  
    26.     public void LoadMenu()
    27.     {
    28.         Time.timeScale = 1f;
    29.         SceneManager.LoadScene("Start Menu");
    30.     }
    31.  
    32.     public void QuitGame()
    33.     {
    34.         Debug.Log("Quitting game...");
    35.         Application.Quit();
    36.     }
    37. }
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5. using TMPro;
    6.  
    7. public class CountdownTimer : MonoBehaviour
    8. {
    9.     float currentTime = 0;
    10.     float startingTime = 5;
    11.  
    12.     [SerializeField] TMP_Text countdownText;
    13.  
    14.     void Start()
    15.     {
    16.         currentTime = startingTime;
    17.     }
    18.  
    19.     void Update()
    20.     {
    21.         currentTime -= 1 * Time.deltaTime;
    22.         countdownText.text = currentTime.ToString ("0");
    23.  
    24.         if (currentTime <= 0)
    25.         {
    26.             currentTime = 0;
    27.         }
    28.  
    29.         if (currentTime == 0)
    30.         {
    31.             FindObjectOfType<DeathMenu>().DeathMenuPopUp();
    32.         }
    33.     }
    34. }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,959
    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: https://forum.unity.com/threads/how-to-capturing-device-logs-on-ios.529920/ or this answer for Android: https://forum.unity.com/threads/how-to-capturing-device-logs-on-android.528680/

    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:

    https://forum.unity.com/threads/coroutine-missing-hint-and-error.1103197/#post-7100494
     
    jigginsrult likes this.
  3. TheFunnySide

    TheFunnySide

    Joined:
    Nov 17, 2018
    Posts:
    192
    My assumption is that the CountdownTimer calls FindObjectOfType<DeathMenu>().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.
     
    jigginsrult likes this.
  4. jigginsrult

    jigginsrult

    Joined:
    Apr 15, 2021
    Posts:
    2
    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.

    Code (CSharp):
    1. if (currentTime == 0 && timeIsUp == false)
    2.         {
    3.             Debug.Log("Time has reached 0");
    4.             timeIsUp = true;
    5.  
    6.             if (timeIsUp)
    7.             {
    8.                 FindObjectOfType<DeathMenu>().DeathMenuPopUp();
    9.             }
    10.         }