- Home /
Skipping over timer
Hello, I have a system where when the game starts a gui will say "foo" and after 5 seconds it change's to "bar", but its just going to bar without waiting 5 seconds.
Code
void Update () {
text1.text = "foo";
wait (5);
text1.text = "bar";
}
IEnumerator wait(int time)
{
yield return new WaitForSeconds(time);
}
Answer by Dave-Carlile · Jul 27, 2015 at 09:17 PM
That's not quite how Coroutines work, and you can't do waiting directly inside Update.
Try this:
void Start()
{
StartCoroutine(FooBar(5));
}
IEnumerator FooBar(float time)
{
text1.text = "foo";
yield return new WaitForSeconds(time);
text1.text = "bar";
}
I moved the StartCoroutine call to Start because you don't want to call that every frame or you'll be starting a new coroutine each frame. If you need to start it in Update then you'll want some condition in place so it only starts it when needed.
Your answer
Follow this Question
Related Questions
Countdown Timer or WaitForSeconds? 2 Answers
Time.deltaTime appears to be running fast 1 Answer
2D GUI accessibility 2 Answers
making and integrating Visual novel element in 3d game? 2 Answers