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
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.
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?
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.