- Home /
 
How to debug a coroutine?
I'm trying to fire an IEnumerator (C#) routine from Update. I can see that the line gets there in Update, I set a break point there ok. But breakpoints in the coroutine are never hit, and nothing seems to happen. It's like the routine is not called, and I don't know why.
 public class FrameUploader : MonoBehaviour
 {
     public Texture2D texture;
     public float frameRate = 1f;
     public string prepend = "user0_";
     public int frameNum = 0;
     public bool showing = true;
     public string server = "http://myserver/uploadimage.php";
     
     float lastFrameTime = 0f;
     int q = 0;
         
     // May want to do this on physics update or late update?
     void Update ()
     {
         if ((Time.time - lastFrameTime) >= 1f/frameRate)
         {
             lastFrameTime = Time.time;
             QueueTexture(); // It gets here, if I set a breakpoint here
         }
     }
     
     IEnumerator QueueTexture()
     {
         // Cannot stop at any breakpoints here. 'q' never changes according to OnGUI
         WWW wwwToUpload = null;
         byte[] bytes = texture.EncodeToPNG();
         WWWForm form = new WWWForm();
         form.AddField("frameNum", frameNum);
         form.AddBinaryData("fileUpload", bytes, prepend+frameNum+".png", "image/png");
         wwwToUpload = new WWW (server+frameNum, form);
         q++;
         frameNum++;
         yield return wwwToUpload;
         wwwToUpload = null;
         form = null;
         q--;
     }
     
     void OnGUI()
     {
         if (showing)
         {
             GUI.Label (new Rect (10, Screen.height-30, 200, 20), "Q: "+q);
         }
     }
 }
 
              Answer by Bunny83 · Aug 30, 2013 at 06:47 AM
You didn't start the coroutine. You have to used StartCoroutine
     StartCoroutine( QueueTexture() );
 
              I had put 'return yield StartCoroutine' as seen in an example in the manual, which didn't work, so I abandoned that. So close. Thanks.
Well, that's because "return yield" doesn't exist but "yield return" ;).
However when you use yield return the calling function will also be a coroutine. You can't use yield in a normal function.
Answer by dreasgrech · Aug 30, 2013 at 08:48 AM
QueueTexture returns an IEnumerator, and you're ignoring that return value; that's why nothing is happening. The coroutine is not being started.
You will need to start it with StartCoroutine(QueueTexture());
which is the same as
 IEnumerator queueTextureCoroutine = QueueTexture();
 StartCoroutine(queueTextureCoroutine);
 
              Your answer