How to terminate script without killing the whole Unity

I am working on a script that controls a vehicle - things are going well, but every now and then I run into a situation where while the vehicle is moving tracking its target, the whole Unity environment freezes; windows don’t update, and there is no response.

If I wait for a while (minutes), I get an error “Quaternion To Matrix conversion failed because input Quaternion is invalid” message at the lower left corner of the window. While all of this is going on, the Unity process shows 100% (in fact 25%, as I have 4 CPUs) and the only way to get out of this mode is to kill the Unity process.

My whole setup is a bit involved to post, but I am wondering is there a way to terminate a script without killing the whole tool? I have narrowed down the ‘problem’ in a single subroutine; if I rearrange the code the error goes away but will show up at another location and there isn’t many ways to debug this.

Any suggestions are appreciated.

If the editor is unresponsive, you can use the keyboard shortcuts to interrupt the player.
(Ctrl or Command) + P will stop the scene.
(Ctrl or Command) + Shift + P will Pause the scene.
I find this can be quite useful, and if you can actually pause the scene successfully you can quite often get in and find some debug information.

Unfortunately, nothing is responsive; keystrokes do nothing, in fact, the window doesn’t update either; it’s as if the whole process is running an endless loop with high priority, preventing anything else in that process from running.

The only way to get past it is to kill the process from the process manager.

sounds like you have something that ramps up the ram requirement.
in that case just wait a while and unity will become responsive again and you can terminate the play mode.

I’ve seen that when I had a “slightly” errorous script

It’s not that either; the CPU that Unity is running on goes to 100% but RAM usage remains the same; also, any new page faults quit, which means Unity is stuck in an endless loop internally; my script has no loops so it’s not stuck there.

I think something is going on within the physics engine but it’s almost impossible to debug.