Countdown Timer Not Resetting on Game Load
Posted: Mon Oct 23, 2023 10:14 pm
Hi Tony,
I’m having trouble with the countdown timer in my game.
The countdown timer script:
using TMPro;
using UnityEngine;
using UnityEngine.Events;
public class CountdownTimer : MonoBehaviour
{
public float countdownTime;
public TMP_Text countdownText;
public UnityEvent onCountdownFinished;
private float currentTime;
private bool isCountingDown;
private static CountdownTimer instance;
private void Awake()
{
if (instance == null)
{
instance = this;
DontDestroyOnLoad(gameObject);
}
else if (instance != this)
{
Destroy(gameObject);
return;
}
}
private void Start()
{
ResetTimer();
}
private void Update()
{
if (isCountingDown)
{
currentTime -= Time.deltaTime;
UpdateCountdownUI();
if (currentTime <= 0)
{
isCountingDown = false;
onCountdownFinished.Invoke();
}
}
}
private void UpdateCountdownUI()
{
int minutes = Mathf.FloorToInt(currentTime / 60);
int seconds = Mathf.FloorToInt(currentTime % 60);
int milliseconds = Mathf.FloorToInt((currentTime * 100) % 100);
countdownText.text = string.Format("{0:00}:{1:00}:{2:00}", minutes, seconds, milliseconds);
}
public void StartCountdown()
{
ResetTimer();
isCountingDown = true;
}
public void ResetTimer()
{
currentTime = countdownTime;
UpdateCountdownUI();
}
It works fine during gameplay and restarts correctly with OnRestartGame(). However, when I save and then load the game, the timer doesn’t reset and continues counting down.
In the dialogue system, node sequence window, I activate the timer using:
SetActive(3MinsCounDownText, true);
SendMessage(StartCountdown, , 3mins_CountdownTimer);
Do you have any ideas on how to fix this so the timer resets properly when loading a saved game?
Thanks!
I’m having trouble with the countdown timer in my game.
The countdown timer script:
using TMPro;
using UnityEngine;
using UnityEngine.Events;
public class CountdownTimer : MonoBehaviour
{
public float countdownTime;
public TMP_Text countdownText;
public UnityEvent onCountdownFinished;
private float currentTime;
private bool isCountingDown;
private static CountdownTimer instance;
private void Awake()
{
if (instance == null)
{
instance = this;
DontDestroyOnLoad(gameObject);
}
else if (instance != this)
{
Destroy(gameObject);
return;
}
}
private void Start()
{
ResetTimer();
}
private void Update()
{
if (isCountingDown)
{
currentTime -= Time.deltaTime;
UpdateCountdownUI();
if (currentTime <= 0)
{
isCountingDown = false;
onCountdownFinished.Invoke();
}
}
}
private void UpdateCountdownUI()
{
int minutes = Mathf.FloorToInt(currentTime / 60);
int seconds = Mathf.FloorToInt(currentTime % 60);
int milliseconds = Mathf.FloorToInt((currentTime * 100) % 100);
countdownText.text = string.Format("{0:00}:{1:00}:{2:00}", minutes, seconds, milliseconds);
}
public void StartCountdown()
{
ResetTimer();
isCountingDown = true;
}
public void ResetTimer()
{
currentTime = countdownTime;
UpdateCountdownUI();
}
It works fine during gameplay and restarts correctly with OnRestartGame(). However, when I save and then load the game, the timer doesn’t reset and continues counting down.
In the dialogue system, node sequence window, I activate the timer using:
SetActive(3MinsCounDownText, true);
SendMessage(StartCountdown, , 3mins_CountdownTimer);
Do you have any ideas on how to fix this so the timer resets properly when loading a saved game?
Thanks!