Spawning units using the Object Pooling pattern
The Object Pooling pattern was created to optimize memory usage and CPU performance by reusing objects that are pre-allocated in a pool, instead of instantiating a new object when needed and then destroying it. When the ObjectPool
script is created, a fixed number of instances will be allocated and added to a list. Then, when an object is required, the Object Pool will give you an existing copy. When you do not need the object anymore, for example when a unit is destroyed by the enemy, the object is returned to the Object Pool.
We can use the Object Pooling pattern when we frequently need to create and destroy objects that are all the same or very similar. A good use case is bullets that are fired from a weapon; they are all the same and created and destroyed a lot. In this case, the Object Pooling pattern will reduce the overhead on the CPU to create and destroy an object as well as making it faster to reuse an object instead of instantiating...