Skip to content

Instantly share code, notes, and snippets.

@beardordie
Created December 6, 2023 07:22
Show Gist options
  • Save beardordie/043b1d62e1746c36b17b2ccd8f16b7fd to your computer and use it in GitHub Desktop.
Save beardordie/043b1d62e1746c36b17b2ccd8f16b7fd to your computer and use it in GitHub Desktop.
How To Force Exit Play mode in Unity when stuck inside an infinite loop - Unity Freeze - Visual Studio - Immediate Window trick fix
// If you're in an infinite loop in Unity, it will hang, and you won't be able to hit the Play button to exit Play mode.
// If you happen to be attached to the Visual Studio debugger, and if you can hit a breakpoint somewhere in the executing infinite loop,
// You have hope!
// You can use Immediate Window to issue a command that will break you out of the infinite loop
// After the breakpoint is hit in Visual Studio, open up the Immediate Window (Debug->Windows->Immediate Window)
// Type this command and hit enter:
// DestroyImmediate(this);
// It will give an error in Unity, and it will destroy the offending GameObject with all its scripts including the infinite loop script
// But now you can hit the Play button to exit Play Mode.
// Now fix that dang script before running it again!
// -Beard or Die
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment