Recently, I made a very dumb mistake that caused Unity to crash my computer. I could move the mouse sometimes, but everything else became completely unresponsive, leading to me having to shut off my PC.

What happened: I had an object, let's say an arrow, that could collide with another object, let's call it spawner. When the arrow collides with the spawner, it's supposed to destroy the arrow that activated the spawner. The spawner would then, as the name might imply, spawn more arrows. The problem was that I forgot to temporarily disable to collision on the newly spawned arrows, meaning that as soon as they would spawn inside the spawner's collision box, they'd spawn more arrows, and those arrows would spawn more arrows, and those arrows would... you get the idea. It was a very stupid mistake I realized the moment I hit play, but it was too late.

Now, I know that was entirely my fault, but I was wondering if you could prevent Unity from sending your PC into a coma like that? (Besides not making a dumb mistake like that, of course...) I'll be honest, I kind of expected Unity would have some sort of fail safe for this kind of error. Is there a way to limit the object count for example?

  • \$\begingroup\$ I’m not sure how it works but there is a “Panic Button” in the asset store which apparently breaks out of infinite loops: assetstore.unity.com/packages/tools/utilities/… but unfortunately: it is not free, and apparently doesn’t work in Unity 2018 \$\endgroup\$
    – Ed Marty
    Jan 30, 2019 at 1:10

1 Answer 1


There is far more than one way to put Unity into an infinite loop which freezes both the game and the editor. So limiting the maximum number of objects (if that's actually possible, I don't think it is) would only protect you from a very small subset of possible mistakes.

But you can use a trick built into Windows to prevent Unity from using all of your CPU cores. The other cores are then still able to serve any other tasks, like handling the Windows UI system.

While Unity is running:

  1. Open the Windows task manager with Ctrl+Shift+Esc
  2. Go to the "Details" tab
  3. Right-click on the Unity process
  4. Select "Set Affinity..."
  5. Uncheck one of your CPU cores
  • 1
    \$\begingroup\$ "limiting the maximum number of objects (if that's actually possible, I don't think it is)" it's not unfortunately. You can always to that from your script though. \$\endgroup\$
    – user115399
    Jan 28, 2019 at 15:30
  • 1
    \$\begingroup\$ @GabrieleVierti I think that's easier said than done. Counting the object you already have in your scene is expensive, and simply keeping count of instantiated objects doesn't account for objects getting destroyed. It would be easy to do if you use object pools, but those are not a good solution for every possible situation. But if you have a good idea how this could be done with minimum work and side effects, you could post an answer with some example code. \$\endgroup\$
    – Philipp
    Jan 28, 2019 at 15:34

You must log in to answer this question.