Debugging Unity Editor lockups


Somehow during the development process I coded myself into a corner where the Unity editor would freeze after a second or two of game play whenever I pressed play - no errors, no crash-log, just a total lockup with nothing that can be done apart from killing the process and starting over.

As you can imagine, this has slowed down development a tad!

I spent a lot of time messing about on this - everything from updating the Unity version and A* library to fiddling with the system clock to get a new seed for the random number generator.  I wanted to just leave a note here about the problem I faced, and how I eventually found a way to debug these issues since it may be useful for others (or just for myself if this happens again in the future).

Checking the Unity Editor Logs

If you get a proper crash in the unity editor, you'll generally want to go and take a look in the log files the Unity editor writes to disk.  For me on Windows, these files live here

C:\Users\<user>\AppData\Local\Unity\Editor

For me, everything looked fine - there was no real indication of anything unusual happening.  I was not seeing any errors or anything weird, so it didn't help me much.  I was stuck with a locked-up Unity and no clues.

Debugging via Visual Studio

I lucked onto a solution that worked for me though - despite the UI of Unity being entirely locked up, the debug server was still running. This meant I could attach the debugger (either directly before hitting Unity's play button, or even after Unity had locked up).  Once attached, I could hit the "pause" button in Visual Studio and voila, I had paused execution and was sitting in the debugger and was able to step-through the code as usual as if nothing had happened!

In my case it turned out that each time the NPC reaches its destination, it tries to find a new destination.  I had a simple loop that picked a destination at random in a loop that would repeat if the "new" destination was the same as the "old" one.  Turned out that somehow despite there being many way points in the game for each NPC to pick from, there was 1 (and only 1!) NPC that for some reason kept on picking the same destination over and over and over again, meaning the loop never exited.  This was because the poor NPC was actually in a tiny disconnected graph with only one waypoint so it could only ever pick the same one, meaning there was no way to exit the loop.


Here in the screenshot you can see the dark blue overlay that shows the navigation graph, and the NPC currenlty selected - the navigation graph at the cross-roads of the corridor is actually disconnected from the rest of the graph, and there happens to only be one way points in that area meaning there was no where to go. This also explains why the lockup happened a couple of seconds into the game - the NPC's starting position is a couple of seconds walk away from the waypoint - when it reached it and started looking for its next destination it then entered the infinite loop.

I fixed this issue (it was a problem with some "meta entities" (navigation waypoints, console messages etc) being on the wrong layer.  To prevent this happening again I also put in a quick workaround was to put in a failsafe counter inside the navigation loop to abort finding a new destination and log a warning.

Development can now continue!

Get Mechfight Remake

Comments

Log in with itch.io to leave a comment.

Thank you for this. I also had a "no-error" lockup, and based on your post, I looked at possible problems with pathfinding for AI, and found the problem. Thank you!