A
A
AlxWanderer2019-01-22 20:56:45
Unity
AlxWanderer, 2019-01-22 20:56:45

After pressing Play, you need the game to be paused and after 5 seconds (reverse counter) the game starts, how to do this?

After pressing Play, you need the game to be paused and after 5 seconds (reverse counter) the game starts, how to do this?
Here is a script, it defends, but the game is on, how to fix it?

public int timeLeft = 5;
    public Text countdownText;

    // Use this for initialization
    void Start()
    {
        StartCoroutine("LoseTime");
    }

    // Update is called once per frame
    void Update()
    {
        countdownText.text = ("" + timeLeft);

        if (timeLeft <= 0)
        {
            StopCoroutine("");
            countdownText.text = "";
        }
    }

    IEnumerator LoseTime()
    {
        while (true)
        {
            yield return new WaitForSeconds(1);
            timeLeft--;
        }
    }

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
AlxWanderer, 2019-01-22
@AlxWanderer

Tried through Time.timeScale, but there is a problem that the whole level rises, including the timer!

A
Alex Maximovich, 2019-01-24
@flexer1992

you can try something similar.

private int _timeLeft = 5;
public Text countdownText;
private float _currentTime;

void Start()
{
    _currentTime = _timeLeft;
}

void Update()
{
    _currentTime -= Time.deltaTime;   
    countdownText.text = (int)_currentTime.ToString());    
    if(_currentTime <= 0)    
    {
        Debug.Log("StartGame");
    }
}

B
Brown2Fox, 2019-01-25
@Brown2Fox

In ScriptsExecutionOrder, set the highest priority to this script, and then check the IsStarted variable with another script (say, a global game manager that initializes all sorts of things for the game)

public class DelayedStart: MonoBehaviour
{

public int count = 5;
public static bool IsStarted = false;

void Awake()
{
    StartCoroutine(StartCounting());
}

private IEnumerator StartCounting()
{
    for (int i = count; i > 0; i--)
    {
         yield return new WaitForSeconds(1f);
         Debug.LogFormat("Time Left: {0} s", i);
    }
    IsStarted = true;
}
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question