Please help - destroy

Ive asked his before with no response.

My game crashes after a couple of times. I go through the level once or twice and then on the third time it’ll crash. After debugging I have found out that its the enemy in my game. The enemy is one prefab spawned and destroyed using Destroy() a number of times in each level.

I think that the memory is getting used up and causing a crash. Is there a better method than destroy to get rid of a game object completely out of iPhone memory?

Thanks!

Obvious question. Why are you destroying the object instead of reusing it. Destroy isn’t instant so it is possible that you are creating objects and filling the memory instead of using a pool pattern.

Im destroying them because the player fires at them and they get destroyed, is there a better way of doing this?

as peter mentioned: recycle them

To elaborate slightly, when your enemy dies, don’t destroy it, hide it, reset all its states etc so it can be used again.

Instantiating things gets expensive very quickly.

I find that instantiate/destroy is slower than simply having a pool of enemies/projectiles hidden off screen until needed, but it shouldn’t cause crashes. What’s your total memory usage when the app is running? Try running it with activity monitor in Xcode to see how much memory its using at its peak - you might just have too many textures and sounds period, instantiating aside.

Its entirely possible that you’re just right on the verge of crashing to begin with, and during runtime your mono heap grows a bit and… kaboom.