- Home /
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.
If it becomes too heavy you can create a queue system for it. You could hide all your objects that you want to delete so they dont render and add them to the queue and just have them delete one after another ins$$anonymous$$d of all at once.
20 objects doesn't seem like a "massive" amound. Anyway, try using a Coroutine to divide the destroying of the parts over several frames so there are less obejcts destroyed per single frame.
The moderator queue must be quite backed up I guess... my answer is in limbo right now but I was going to suggest you implement a simple object pool to rule out too many Instantiate() or Destroy() calls causing performance related errors.
I haven't seen that specific error you mention but strange things happened in my programs with too many of those expensive calls causing lag. I fixed it by implementing object pooling. Good luck!
Are you using the latest version of Unity? Unity 5.3 had this problem (according to http://answers.unity3d.com/questions/950569/game-runs-in-editor-crashes-on-startup-after-build.html?childToView=951281#answer-951281)
You should also look at the stack overflow post here: https://gamedev.stackexchange.com/questions/110613/my-game-crashed-access-violation-0xc0000005
Lastly, if you haven't already fixed the problem, can you tell me how much memory you have in you're computer. If you're using an old system, you may have memory problems. This is such a low-level problem that I may not even be able to replicate it and will require you to investigate on your own. I am no authority in these subjects. Good luck!
I've destroyed hundreds of objects at once, perhaps more is going on here? Are your objects running any code in OnDisable(), OnDestroy(), or ~ObjectsName()? Do any of these objects contain other objects that are from inaccessible DLL libraries?
Answer by Propagant · May 07, 2017 at 02:38 PM
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 ;P 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.
You saved my day Propagant, I was stuck with this for 3 months.
Propagant is correct.. instantiate and destroy are very slow.... you will notice hitches and drops in framerate if you use them alot.... Its better to move them on and off screen, far from camera, and/or use object pooling if they can be re-used. If they cant be re-used... move them far from camera, and don't destroy until end of level.... if you must destroy... then add them to a list... and destroy them one per frame in a coRoutine... not all at once.
Answer by dashticle · May 09, 2017 at 05:20 AM
Hi,
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!
Good Luck.
Answer by salamander555 · May 07, 2017 at 09:21 AM
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.
Your answer
![](https://koobas.hobune.stream/wayback/20220612121341im_/https://answers.unity.com/themes/thub/images/avi.jpg)