Hi, these are my code
using UnityEngine;
using System.Collections;
public class sample : MonoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (Input.GetKeyDown("space"))
{
print ("called before coroutine");
StartCoroutine("testCOR");
print ("called after coroutine");
}
print ("this is end of frame.");
}
IEnumerator testCOR()
{
for(int i=0;i<3;i++)
{
print ("Now number is: "+i);
print ("Just before coroutine");
yield return null;
}
}
}
When I press spaceBar just one time, the result pattern I am expecting is:
....................
....................
this is end of frame.
this is end of frame.
this is end of frame.
called before coroutine
Now number is: 0
Just before coroutine
called after coroutine
this is end of frame.
called before coroutine
Now number is: 1
Just before coroutine
called after coroutine
this is end of frame.
called before coroutine
Now number is: 2
Just before coroutine
called after coroutine
this is end of frame.
called before coroutine
called after coroutine
this is end of frame.
this is end of frame.
this is end of frame.
In stead I am getting the following:
....................
....................
this is end of frame.
this is end of frame.
this is end of frame.
called before coroutine
Now number is: 0
Just before coroutine
called after coroutine
this is end of frame.
this is end of frame. //why twice. Is unity taking two frames?
Now number is: 1
Just before coroutine
this is end of frame. //where is "called after coroutine"?
Now number is: 2
Just before coroutine
this is end of frame.
this is end of frame.
....................
....................
When I press spaceBar again, I got these:
....................
....................
this is end of frame.
this is end of frame.
called before coroutine
Now number is: 0
Just before coroutine
called after coroutine
this is end of frame.
this is end of frame.
Now number is: 1
Just before coroutine
this is end of frame.
Now number is: 2
Just before coroutine
this is end of frame.
this is end of frame.
....................
....................
The dots means I am getting “this is end of frame.” line continuously forever.
So, each time I press spaceBar, I get similar pattern. Probably coroutine does not work like the way I thought. But how does they execute? I need to know this. It is now such a mystery to me now. I thought like these:
when you encounter yield statement, control flow jumps from coroutine to next line of “startCoroutine()” and then finishes rest of the jobs in update() method. After finishing, flow returns(of course after lateUpdate and other stuffs) to again Update method. This time flow goes to co-routine again. But “with having previous state”. Loops goes on and on until the “forLoop” does not let it go further. Then flow again returns to update method and completes the rest jobs to render that frame. This happens whenever something triggers the coroutine.
If we use coroutine’s yield like “waitforFixedUpdate” manner,then execution resume on next physics change instead of next frame. If we use “waitForEndOfFrame”, then execution resume after LateUpdate and rendering(GUI drawing?) on next frame in stead of next frame on “update” method. That means coroutine will not execute while we are on update method if we use “waitForEndOfFrame”. Rather it will execute when we finish drawing our frame.
So those were my thoughts on coroutine. Clearly I was wrong. But what is right regarding Coroutine. Please shed some lights on it. It is driving me crazy!