How to spawn enemies


Im currently trying to decide how to manage enemies spawning into my little game. Basically Im going to have enemies spawning in waves and the player needs to take them out in time or they get overwhelmed.
So now Im trying to decide between 2 different ways to do this and I’m unsure what is the best approach.

  1. have a enemyfactory with a set amount of enemies that will be reused.
  2. Put out all enemies in a wave as child of the wave and activate them when enemywave is started.

I’m targeting handhelds/tablets so performance is pretty important.

Avoid instantiation. Use pools and reuse objects. Some objects may be expensive to even activate/deactivate fully so profile your game for these events. Depending on your complexity, it may be faster to turn off behavioural scripts (or even just use a boolean to early return) and moving the objects off screen. It’s hard to tell exactly what the best approach is as it’s dependent on your setup but don’t be afraid of experimenting a little with mock objects and monitor performance for it.

An example would be that you know your characters will have (presumably) mesh, animation, rigidbody, colliders and other components. Take the worst case scenario and deactivate/activate all of them at once and see how much time it takes. You don’t need to have end grade meshes or meaningful scripts on them as long as they are representative of a typical load that you’ll face. Then try to de/activate one per frame and see how that goes. For example, if you notice that deactivating or activating a lot of objects at once produce a spike, consider queuing them up to mitigate the cost.