r/UnityHelp • u/jollyolly2137 • Aug 29 '21
Trying to understand Corutines
Shouldn't this pritnt "Text" in the console every 3 three seconds?
IEnumerator test()
{
Debug.Log("text");
yield return new WaitForSeconds(3);
}
I ofc start corutine in update.
It spams the output every frame rather than every 3 seconds
1
u/Canamla Aug 29 '21
If you want to run it from Update, check against a condition when you want to run the coroutine, make that condition false while it runs, then make the condition true at the end so it can repeat (if desired). A bool is probably the simplest.
1
u/jollyolly2137 Aug 29 '21
Yea, that was a weird and stupid mistake.
1
u/Canamla Aug 29 '21
Lol I've had my fair share of frustrations with it. Took me longer than I'd like to admit to figure out how to use them properly :p
1
u/Asylation Helped Community Aug 29 '21
https://docs.unity3d.com/ScriptReference/WaitForSeconds.html
"See WaitForSecondsRealtime if you wish to wait using unscaled time."