Object Pooling

Stephen York
6 min readMar 20, 2021
No Object Pool

So at some point in your development journey you will reach the place of wanting to create multiple instances of the same object throughout the life of your program. It could be in the creation of a tower defense game dealing with the enemies, or just the bullets that you want to fire from a gun. The point is that they are prefabs that you want to create and then destroy in an endless cycle.

How do you handle it? You don’t really want to instantiate an instance of the prefab every time you need it and then destroy it when you no longer want it. That could be the creation and destruction of the bullet prefab many times a second, which is not very optimal or clean. So what to do then?

Enter object pooling.

Object pooling is the reuse of a set of prefabs already created. At the start of the game, you create a set of 20 bullets, none of which start as active. Then when you want a bullet, you only have to activate one of those bullets instead of creating one and deactivate it instead of destroying it. You keep reusing the same bullets for the life of your game by activating and deactivating them.

Let’s walk through a way to set up objecting pooling with the example of lasers.

Step 1: Create PoolManager

To start, create a C# script and name it something relevant, like PoolManager. Then create an empty game object and give it the same name as your script and attach your script to it. And then open up your script to begin coding.

Step 2: Create Initial Variables

Let’s think about what we’ll need to create our object pool. Well we know that we’ll need a reference to our laser so we’ll create a game object variable named “_laser” and make it private since no other class will need access to the laser. And since we’re creating a pool of lasers, we will want some way to hold them, so perhaps a list or an array of game objects. I am going to use a list of game objects instead of an array for some flexibility of not being stuck with a certain size. Let’s serialize both of these so that we can assign the laser in the inspector and be able to see our list of lasers. That’s probably all that we need to get started, but we may need to create some other variable later on.

Step 3: Create Laser Pool

Alright, to create the laser pool we will we create two custom methods. The first will handle the creation of the individual lasers and the second will handle the pool of lasers.

The first method, called CreateLaser(), has a return type of GameObject and handles to creation of the individual lasers. To start, we will create an instance of the laser prefab. Instantiate() takes in the reference to the prefab, the position in which we want it to be created, its starting rotation, and its parent object (if we want it to be a child object). To keep our hierarchy clean, I want to child all of our lasers to an empty game object. We need to create two more variables, the starting position and the laser container. Let’s create a _spawnPos of type Transform and a _laserContainer of type GameObject and serialize them both to assign them in the inspector. We create the instance of our laser prefab at the spawn position with the laser’s initial rotation and child it to our laser container. And let’s store that in a local variable of type GameObject and call it laser. Ignore the bool for now, we will touch on it later, but we will set the laser GameObject inactive so it isn’t just sitting in our scene and add it to the laser pool. And then since CreateLaser() has a return type we will return the laser GameObject.

Now we get into our GenerateLaserPool() method. This method is responsible creating the pool of lasers and it has a return type of a game object list. And we find ourselves in need of a new variable: our starting amount of lasers. Let’s create an integer variable, named _laserQuantity, and initialize to 10. Now let’s create a for loop and starting at zero, iterate through it until we reach our _laserQuantity. For each increment, we call our CreateLaser() method to create each individual laser, and once the for loop finishes, we return our laser pool list which is now fully populated with lasers.

Game start

Step 4: Access Lasers in Pool

Alright, now comes the time to use the lasers that are in our pool. For this we create one more method, called GetLaser() which will return a game object.

You will call this method whenever you want to get a laser, so when you shoot your laser gun. This method uses a foreach loop to look at the lasers in the laser pool and if it finds a laser that is not active, it will set it active and return it. And that’s all you have to do to get a laser from the pool. But there are two more lines of code in this method.

And you notice that the CreateLaser() method gets returned after the foreach loop. And just in case that our pool is not big enough for the amount of lasers be flung around, which means our foreach loop does not return any lasers, we need more lasers right now. By returning the CreateLaser() method, we can add lasers to our pool at runtime if we need them so our game does not have to wait for one of the already active lasers to be deactivated. And now let’s mention that bool that I told you to forget earlier. This bool, titled _initialCreation, handles whether or not the laser is set inactive when it is first created. At the start of the game when the CreateLaser() method is first called, _initialCreation is set to true, and if _initialCreation is true, then the laser is set inactive when it is created. But in the GetLaser() method, the bool is set to false. That way, when CreateLaser() is called GetLaser(), when the player needs a laser at that moment, the laser is not set inactive but instead gets used immediately.

Step 5: Deactivate Lasers

The last step is nice and simple. Create another method that has a game object parameter. All this method will do when called is set the game object that gets passed in to be inactive. Call this method whenever your laser hits something, or whenever you want to get rid of the laser.

And that raps up object pooling.

Lasers being activated on space key and deactivated after exiting screen

--

--

Stephen York

An Atlanta based Unity developer, who enjoys running, reading, and creating videogames!