Search code examples
unity-game-engineframe-rate

Sudden frame rate drops on mobile when instantiating/destroying?


I am currently working on a mobile game and I can't help but notice that whenever an object (an obstacle in my case) is instantiated or destroyed, I get a sudden FPS drop which is critical for my gameplay.

To help give an idea, I instantiate obstacles on top of my screen every 1.5 seconds, then I scroll them down. If the obstacles reaches the bottom of the screen already, I destroy them to prevent memory leaks/waste.

I'm still pretty new to Unity development. Am I on the right track though? What is a better solution to prevent this sudden frame rate drop?


Solution

  • Do you have any big / nested loops or complex processes going off as part of instantiation (Look at your awake/start methods)?

    Regardless, look into object pooling as a better method to handle this type of thing.

    For a basic example, instead of creating/destroying projectiles of a gun every time it's used, give that gun a "projectile pool" that creates n projectiles when the level loads. Then, when shooting, just set the projectile's position back to the gun and set the projectile as active. After impact, have the projectile deactivate (or after a few seconds if nothing is hit).