Hello guys!
I found that only one coroutine can wait for another coroutine! This behavior is very rare, since coroutines are a simulation for threads, and there are many cases in which you would like to wait for another ongoing operation (like semaphores in threads).
I also send a bug report to Unity, but haven’t receive any response for a long time (about one moth from current date!)
Steps to reproduce:
- Start a coroutine and obtain the Coroutine object.
- Start a second coroutine, pass the first coroutine as a parameter and wait for it using yield.
- Start a third coroutine, pass the first coroutine as a parameter and wait for it using yield.
- The third coroutine never gets to end.
You will receive a message saying:
“Another coroutine is already waiting for this coroutine!
Currently only one coroutine can wait for another coroutine!”
PS: You can also add the attached script code to a GameObject and press play.
using UnityEngine;
using System.Collections;
public class CoroutineTest : MonoBehaviour
{
void Start () {
Debug.Log ("start");
StartCoroutine(startFunction());
Debug.Log ("end");
}
IEnumerator startFunction ()
{
Debug.Log ("begin start function");
Coroutine f1 = StartCoroutine(function1 ());
StartCoroutine(wait_function1 (f1));
StartCoroutine(wait_function2 (f1));
Debug.Log ("end start function");
yield break;
}
IEnumerator function1()
{
Debug.Log ("begin function 1");
yield return new WaitForSeconds(3);
Debug.Log ("end function 1");
}
IEnumerator wait_function1(Coroutine c)
{
Debug.Log ("begin function 2");
yield return c;
Debug.Log ("end function 2");
}
IEnumerator wait_function2(Coroutine c)
{
Debug.Log ("begin function 3");
yield return c;
Debug.Log ("end function 3");
}
}