- Home /
A loop FOR waits for a coroutine to end
How I do that a for loop waits to a coroutine to end before moving to the next step? Until now I got this:
for(int i = 1; i <= 10; i++)
{
yield return StartCoroutine(MyFunction());
}
But it doesn't work... what I'm doing wrong?
EDIT: This code is right, but MyFuntion called another function just like StartCoroutine(mySubFunction) and NOT like yield return StartCoroutine(mySubFunction). That was the problem!
Answer by Loius · Jun 09, 2014 at 10:43 PM
What you have does work. You have to call your function as a coroutine though. Only coroutines are able to pause execution; if a normal function hits a yield statement or if it wasn't started with StartCoroutine it'll just silently exit.
void Start() {
StartCoroutine(SlowLoop());
}
IEnumerator SlowLoop() {
for ( int i = 0; i < 10; i++) {
Debug.Log("Step " + i);
yield return StartCoroutine(MyFunction());
}
Debug.Log("Done");
}
IEnumerator MyFunction() {
yield return new WaitForSeconds(1f);
}
I wrote like this but $$anonymous$$yFuntion called another function just like StartCoroutine(mySubFunction) and NOT like yield return StartCoroutine(mySubFunction). That was the problem!
Your answer
Follow this Question
Related Questions
I can't start a coroutine. I get a weird message from visual studio 1 Answer
Coroutines IEnumerator not working as expected 2 Answers
How to force Coroutine to finish 1 Answer
Startcoroutine not working second time 2 Answers
Coroutines and states 1 Answer