Answer the question
In order to leave comments, you need to log in
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
Tried through Time.timeScale, but there is a problem that the whole level rises, including the timer!
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");
}
}
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 questionAsk a Question
731 491 924 answers to any question