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