Weirdest coroutine crash ever

Hey guys ! really dont want to post here but hey I dont know any further.

Im having this DungeonManager who iterates over all enemies to do thier stuff.

If an enemy can shoot at the player he does so and confirms his action.

If he cant shoot he proceeds to move to a position from where he can shoot at the player.

Both action work perfectly fine as long as there is only one enemy.

If there are two everything still works as long as at least one shoots at the player.

If two enemy want to perform the move action unity crashes and here comes the really weird thing.

The crash then occurs in the following coroutine:

Now the movement is tile based and the “int path” parameter contains the tile numbers from start to destination.
for each step i get the tiles position, rotate the character, and proceed to move him in a while loop to its destination.
after each step I Log it to the console.
Now only if both enemies need to move the first enemy crashes in the while loop before reaching the last step in its path.
(Eg. the path is 2 tiles, I get the first step logged, the character moves to the last tile and crashes before the last log)
That feels like an infinite while loop but

1. its works if the 2nd enemy doesnt needs to move.

2. even if i choose to run the while loop for a set amount of times (like with an int i < X, i++) it still crashes.

Now what is even weirder if the DungeonManagers ConfirmActionFinished method
(gets called when an enemy finished his action) calls his own action (index) over and over again,
No crash occurs even if the secound enemy needs to move aswell.
Its like if the next action should be called the loop crashes and doesnt even reaches the point of calling another action, but if its own action will be called the loop doesnt crashes, terminates fine and moves on.

Dunno how to explain this, its just really weird, the coroutine crashes if after the coroutine another enemy would move and it doesnt crashes if not. as if the coroutine would know whats going to happen afterwards. well thanks for everyone reading/helping !

First of all why do you use such dangerous recursive method calls. Not just your IterateDungeonActions which calls itself but also your calling of ConfirmActionFinished from the coroutine which you start from inside IterateDungeonActions. So you have two potential infinite recursions going on. Specifically if the path length is 0 you will directly call MovementFinished from within the StartCoroutine call because you have no yield in between.

Your code is a prime example for spaghetti code, because it’s almost impossible to follow the control flow. Your self recursion can directly be replaced by a loop. Loops are way easier to read and to follow as the exit condition is usually defined more clearly. Having systems calling each other all the time is also not a great structure. It’s difficult to identiry the actual entry point and where the execution will end (like I said, typical spaghetti). You probably should rethink about your structure.

Infinite recursion is usually not an issue when done inside a coroutine as long as you ALWAYS have a yield instruction that is executed. It will actually break the recursion and essentially unrolls it into an iterative process. Of course this doesn’t solve any conceptional logic issues you may have with all your conditions.