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

Countdown Timer Before Game Starts

Discussion in '2D' started by yeonahn_yi, Mar 21, 2020.

  1. yeonahn_yi

    yeonahn_yi

    Joined:
    Mar 12, 2020
    Posts:
    3
    So I've recently been wanting create a game like flappy bird in order to learn how Unity works but I can't for my life figure out how to stop this script from constantly looping. The next issue is that the script doesn't pause my game at all. I just want a timer of 3 seconds and in that 3 seconds, the game needs to be paused on my primary scene with nothing moving until that timer hits 0. Any suggestions?

    Script I used:
    using System.Collections;
    using UnityEngine.UI;
    using UnityEngine;
    using UnityEngine.SceneManagement;

    public class StartScreen : MonoBehaviour
    {

    public float timeLeft = 3.0f;
    public Text startText; // used for showing countdown from 3, 2, 1

    void Update()
    {
    timeLeft -= Time.deltaTime;
    startText.text = (timeLeft).ToString("0");
    if (timeLeft < 0)
    {
    SceneManager.LoadScene("SampleScene");
    }
    }
    }
     
  2. Lo-renzo

    Lo-renzo

    Joined:
    Apr 8, 2018
    Posts:
    1,323
    One way to stop everything moving is to set Time.timeScale to zero. To unpause, set it back to a normal timeScale (1f). Be aware: scripts need to be written in a timescale-independent manner for this to work, i.e. movement needs to be scaled Time.deltaTime to achieve framerate independence.

    For a timeScale-indepednent count down, use Time.unscaledTime / Time.unscaledDeltaTime.

    After hitting timeLeft < 0, you might also want to set timeLeft to float.MaxValue for safety's sake. I'm not quite sure what you mean by looping; it could be that you keep entering that if-block.
     
    yeonahn_yi likes this.
  3. yeonahn_yi

    yeonahn_yi

    Joined:
    Mar 12, 2020
    Posts:
    3
    So what I mean by looping is that with my current script, the game starts on it own while the countdown starts but when the countdown hits 0, the game restarts from the beginning of the scene and the countdown starts all over again.
     
  4. Lo-renzo

    Lo-renzo

    Joined:
    Apr 8, 2018
    Posts:
    1,323
    To prevent this, you only want to reload the scene if, say, the player dies. So, instead of loading the scene when the countdown ends - and thus re-initiating that loop - you should display the countdown and pausing behavior by a different means.

    You could, for example, use Awake() or Start(). In one of these functions, you set the game to be paused at start by setting the timeScale to 0. Then launch a coroutine (see guide). You can use WaitForSecondsRealtime(1f) several times to update your countdown text. Then once the (realtime) seconds are up, undo the pausing by setting timeScale back to 1. Here, I imagine that your countdown text is overlaid onto the paused scene, as opposed to counting down in some separate scene.