- Home /
yield return M() vs yield return StartCoroutine
I am working with coroutines, and wondering if there is any difference between these two different usages:
public void Update()
{
yield return M();
yield return StartCoroutine(M());
}
public IEnumerator M()
{
// ....
}
Just a note for future visitors. The second answer is the correct and updated answer not the best one when writing this comment.
I removed the accepted answer so second answer is now first with more upvotes.
Answer by andymilsom · Nov 11, 2016 at 02:48 PM
This is no longer the case after an update during 5.3. Unity 5.4.0 onwards creates a coroutine internally if an IEnumerator value is yield return.
yield return M();
and
yield return StartCoroutine(M());
Will now behave the same and run the full function M as a coroutine.
This is scary - where is this documented? I had a look in the manual and release notes for 5.3.1 -> 5.3.7 and 5.4.0 and saw it nowhere.
Agreed. That's a bit scary. Not that it's an unintuitive behaviour, no, people always tried it that way and were confused why it doesn't work. So it's a change to the better. However it's strange that they don't wrote anything in the release notes. Well, it's not a feature that will break any existing code because nobody has handed the coroutine scheduler of Unity an IEnumerator since that was pointless.
Though it would be great to know about such changes since we try to $$anonymous$$ch people all the small quirks about Unity and i don't regularly check all the "non-features" if they have become a feature. It's bad if you have no information on a certain topic, but it's worse to have wrong information.
Anyways, thanks @andymilsom for the info ^^. It just made me download the newest Unity version just to see it that's true, and it is.
Is that last line supposed to say "Will now behave the same..."?
Yes, I'm sure it should be "now" ^^. I've edited the answer since he wasn't on for some time.
Thanks. Re$$anonymous$$ds me of the time my Dad spent 2 days trying to set up his new VCR to record, because of a badly translated manual that said, at the end of the instructions on how to do it, "the clock icon should not be showing".
Answer by PouletFrit · Jun 09, 2014 at 12:50 PM
Yes.
yield return M();
is the same then yield return null;. It won't start the Coroutine and yield for one frame.
yield return StartCoroutine(M());
will wait that the coroutine has completed before resuming your function that called it.
Note: You can't "yield" in Update.
Thanks. I was only giving Update as an example, guess not a good one. According to what you describe, the body of the method that contains the yield call will not continue on until the coroutine has finished (e.g: in my example, the instructions in Update after the call to the Coroutine will not execute, only until the coroutine $$anonymous$$ has finished) ?