- Home /
Unpause game when the timer reaches 0
When the game scene opens, I would like it to be automatically paused and to start displaying a 3 second timer (3 2 1). Once the timer is less than 0, I would like the game to be unpaused so the user can start playing.
This is my code so far :). At the moment, it starts off paused but the timer doesn't count down. Any ideas as to what I can do?
public class StartGame : MonoBehaviour {
public Text countdownText;
public float timer = 3.0f;
// Use this for initialization
void Start () {
Time.timeScale = 0;
}
// Update is called once per frame
void Update () {
timer -= Time.deltaTime;
countdownText.text = (timer).ToString("0");
if (timer < 0)
{
Time.timeScale = 1;
}
}}
Answer by LCStark · Sep 22, 2018 at 04:00 PM
If you set your timeScale
to 0, the Time.deltaTime
is scaled accordingly, so your timer won't count down. Try using Time.unscaledDeltaTime instead.
I changed it to Time.unscaledDeltaTime, but it doesnt count down - when I press play, the game is immediately unpaused?
Hmm, it seems that there is a problem with calling Time.unscaledDeltaTime
in the startup - for several frames the value is all over the place. You can fix that by clamping the value to the maximum deltaTime value:
timer -= $$anonymous$$athf.Clamp(Time.unscaledDeltaTime, 0.0f, Time.maximumDeltaTime);
Your answer
Follow this Question
Related Questions
Unity Time.timeScale is out of range. Any chance to set it to 360? 1 Answer
2nd independent deltaTime and timeScale variables, or a way to mimic this? 1 Answer
How to pause the game but keep an animation of a game object running? 1 Answer
Time does not start counting down when need to 1 Answer
Game doesn't get paused in Release build 0 Answers