Basic Concept of Object Pooling Design Pattern in Unity (No Code)

Habibur Rahman Ovie
2 min readMay 15, 2022

High object cycling games like Bullet Rush, RTS, Endless runner etc game need instantiating and destroying objects very frequently. Instantiating new object cost performance and fragments the RAM. But this is not the worst case. The big issue is destroying objects. Destroying object need more performance because of the garbage collector need to collect them and free the RAM. This would stole your sleep if you are careless in Game programming.

Problem : Now Imagine you are developing a bullet hell game where you need thousands of bullets in the screen at a time and each bullet lifetime is more or less 1/2 seconds. So each time you need to instantiate thousands of bullets and destroy them. It will drop your performance like hell. To solve this problem we need to use object pooling technique.

Solution : We need to Instantiate the objects only once and instead of destroying them , just disable them and reuse them. Just imagine a pool with full of objects and you pick object from that pool whenever you need and return the object into the pool when you do not need that. Like the following pool filled with rubber duck. Whenever we need a duck we will take it from there and when we do not need that we put it back on the pool.This is the basic concept of object pooling.

Pros :
Better performance at runtime — As all the objects are instantiated while game first loads and all the pre instantiated game objects will reuse all over the game during runtime so game do not need to instantiate or destroy objects in runtime.

Cons:
1. Longer loading time — At the very first time pool need to be filled so we have to instantiate huge amount of game objects and it takes some time.
2. Takes time to implement — It need more time to implement object pooling system in your game than using Unity’s default object creation and destroying methods.

Implementation : To implement this we need to follow the following steps.
1. Instantiate certain amount of objects.
2. Deactivate all the objects
3. Save them in a collection like List/Queue
At runtime whenever an object is needed instead of instantiating the object just activate one from the pool and place it in your desired position.
And when an object is no longer needed just deactivate it and put it back into the pool.

--

--