Unity crashes on destroying a massive amount of objects

Hi, I’m having a big problem with objects destroying. I’ve got a space game, you can fly with your space ship around the limited space and there is also a few enemies. All ships are modular, so they are built by the small parts (like lego). And if you hit some parts with your ship, they will be disconnected from the ship that you hit. ‘Destroyed’ parts have a “timer” that tells to the object when the object has to be destroyed. And there is a problem… When a few objects are destroyed in one moment, it’s okay. But when there is a massive amount of objects [for example 20] destroying in a same time, the game crashed and I have an error log, very popular - Access Violation 0xc0000005. So there must be something really bad and really wrong. Is there some ‘memory limitation’ or is there something with performance? Thanks for answer.


You could try setting up a simple object pooling implementation to rule out some kind of errors by performance. Instantiate() and Destroy() are very heavy and should be avoided where possible, and the garbage collection in Unity is apparently not great so calling Destroy() a lot is especially bad.

While I haven’t seen that specific error, my simulator behaves very strangely and throws errors when it tries to do too much in one frame. I got rid of this and alot of performance issues by embracing Object pooling. Check it out!

Guide by Ray Wenderlich

Official Unity Learn guide

Good Luck.

Have you tried to build the game and then test it? to see if its just the limit memory of you unity or something else.

Guys I have already solved my problem. There was a problem with mesh colliders. When you are destroying such a big amount of objects and each object contains its own mesh collider, it could crash. In my case, I was using mesh colliders with a quite complex mesh source. So that’s it. Don’t use mesh colliders in destroying a massive amount of objects. I think that’s the answer :stuck_out_tongue_winking_eye: Thanks.

Pooling? There is nothing with pooling. As I said, I have modular ship that is built by the simple parts and every part has to be destroyed, not pooled. So pooling could be used in another case.