Question about Unity behaviour on Coroutine vs IEnumerator

Hello everyone,

I have a weird behaviour in Unity and I want to understand what’s wrong with my code

public void MyEntryPoint() {
    // some code
    StartCoroutine(myCoroutine());
    // some code
}

private IEnumerator myCoroutine() {
    // some code
    yield return mySecondCoroutine(); // MARK 1
    // some code
}

private IEnumerator mySecondCoroutine() {
    // some code
    yield return new WaitForFixedUpdate();
    // some code
}

Calling MyEntryPoint(); will crash Unity 90% of the time (player and editor)

BUT

if I replace MARK 1 line by
yield return StartCoroutine(mySecondCoroutine());
and change nothing else, it won’t crash anymore

Do you have any idea why?
For sure you could say it depends on the code of those method, but why it won’t crash if I change from chaining yield (IEnumerator) to yielding for another Coroutine?

Let me know,
I’m realy interested to understand the difference here :slight_smile:

Hey, man!
Try this:

  • public void MyEntryPoint() {

  • // some code

  • StartCoroutine(myCoroutine());

  • // some code

  • }

  • private IEnumerator myCoroutine() {

  • // some code

  • yield return StartCoroutine(mySecondCoroutine()); // MARK 1

  • // some code

  • }

  • private IEnumerator mySecondCoroutine() {

  • // some code

  • yield return new WaitForFixedUpdate();

  • // some code

  • }

It should work.

Thank you for your reply,
I know it would because that exactly what I said in my post,
my question is WHY?

I think for the same reason you can’t just call myCoroutine() without StartCoroutine().

Actually yielding a raw IEnumerator inside a Coroutine should work just fine. i’m currently doing so in one of my projects without any issue. Not sure what’s going on in OP’s example, but it should work.

The issue might be in the “// some code” parts.

1 Like

Unfortunately you are right both code (yield chaining or Starting a new coroutine) are similar,
changing one for the other worked for some time but now I experience crashes again.
So the problem is in “some code” part, but as it’s a coroutine and not having problem on play mode (only in the game build) it’s REALY hard to debug.
Any advice to debug a hard crash of the game in build only with not info in the stack trace?

When you say “crash” is it the type of crash where the game freezes and needs to be force quit, or the kind where the process simply dies?

It’s an actual crash with a stack trace that mention my own code nowhere, not an infinite loop or hanging of some sort.
It’s probably a bug inside one of my coroutine but it’s hard to find

You could put your “some code” inside of a try/catch statement, and put a Debug.LogError and/or a breakpoint inside the catch block. Maybe your code is throwing an exception and that’s causing the coroutine mechanism in Unity to spin off the rails.

Otherwise, if you share some of your code here, someone might be able to spot something you’re missing.

Thank you all for your help, I’ve moved to async/await and it’s way more easy to debug now :slight_smile: