using UnityEngine;
using System.Collections;
public class CoroutineExample : MonoBehaviour
{
IEnumerator Start ()
{
print ("Starting " + Time.time);
yield return StartCoroutine (WaitAndPrint ());
print ("Done " + Time.time);
}
IEnumerator WaitAndPrint ()
{
yield return new WaitForSeconds (5f);
print ("WaitAndPrint " + Time.time);
}
}
The result is
Starting 0
WaitAndPrint 5.010554
Done 5.010554
I have two questions?
First, How to understand the return value of function Start(). I used to see the return value of Start() is void. And in my view, Start() only executes once (one frame) by Unity, but yield return seems make Start() function execute in two frames;
Second, I am also confused by the result. I think the result should be
Starting 0
Done 5.010554
WaitAndPrint 5.010554
Because StartCoroutine() starts the function WaitAndPrint(). In function WaitAndPrint(), yield return makes this function pause in this frame and return to Start(). Then Start() continues going on and prints "Done xxxxx". After 5 seconds, WaitAndPrint() resume and prints "WaitAndPrint xxxxx".
Where am I wrong?