Coroutine doesn't continue after yield another coroutine
Hello. I simple code:
public IEnumerator Coroutine1()
{
Debug.Log("Step 1");
if(Score > 0)
yield return StartCoroutine("Coroutine2");
Debug.Log("Last Step");
UploadData();
}
public IEnumerator Coroutine2()
{
Debug.Log("Step 2");
while(displayScore < Score)
{
displayScore++;
yield return new WaitForEndOfFrame();
}
Debug.Log("Step 3");
}
In the debug log i have:
Step 1
Step 2
Step 3
But i don't have "Last Step". Coroutine2 correctly ends, but program doesn't return to previous coroutine. What i'm doing wrong? Please help.
How do you run the first coroutine ?
You must call :
StartCoroutine( Coroutine1() );
There are two ways to call coroutines. Via function call or coroutine name:
StartCoroutine(Coroutine1());
StartCoroutine("Coroutine1");
I know you can use a string to call your coroutine, but it's far less maintanable :
You can't get a reference of your running coroutine
You can't easily find occurence of the function in your IDE
You can't go to the definition of the function in your IDE
(Should I continue ?)
Anyway ... You must call the Coroutine1 and Coroutine2 using the following syntax if you want your first coroutine to wait until the coroutine2 ends.
StartCoroutine(Coroutine1());
// ....
StartCoroutine(Coroutine2());
Simply because Coroutine2()
returns an IEnumerator
used to make your Coroutine1 wait until Coroutine2 ends
Worked for me. "Last Step" shows up after the Steps. Not sure what's wrong for you. I had displayScore and Score set to 999 if that helps. Unity 5.3.4f1
Your answer
Follow this Question
Related Questions
Designing a dialogbox that displays message, returns a custom object and waits for user response 0 Answers
Checking Internet inside a Coroutine 0 Answers
Coroutine: delay transform rotation but start transform movement immediately 2 Answers
yield return for Photon RPC Coroutine 2 Answers
c# Coroutines and Waypoints HELP PLS!!!,C# Coroutine and Waypoints Help pls!!! 2 Answers