Create an object pool in Unity part 1

Daniel Kirwan
4 min readNov 16, 2021

Objective: Create an object pool to increase game performance

If you would like to try the pooling system out for yourself with having to write it all, you can download it here: https://dangrygames.itch.io/unity-simple-object-pooling-tool Just drag it into your Unity project and try out the demo scene

Object pooling is a great way to increase the performance of a game that requires constant use of instantiating game objects. With object pooling, instead of creating and destroying game objects over and over we simply create them once and then turn them on and off.

I have a 2D space shooter that needs to fire a lot of lasers, from the player, turrets and enemy ships so far. Instantiating all of these objects over and over was creating a lot of garbage for the game to clear up again and again.

To start this off, create and empty game object in your scene and then name it ObjectPool. Now, create a new C# script of the same name and attach the script to the game object.

The first thing to focus on is creating a pool that only takes one gameobject and we extend the functionality in another article to accommodate as many gameobjects as you wish.

In the new ObjectPool script create the above variables. We want these to be public so that other scripts can get access to the variables at run time and turn on the objects we need, when we want to. You don’t really need the last one, I just like to keep my hierarchy clean. This variable will move all objects on the initial instantiation to the poolPosition.

Next, we want to make sure that there is only one instance of the pool in the scene. So, lets turn this into a singleton.

Now, we need to create a for loop that will take the gameobject from the pool list and then create as many as set in the inspector.

In the start method, will are creating a temporary gameobject and assigning it to the gameobject in the pool, we then set it to be deactivated and then add it to the pooledObject list. If you save the file, go into Unity and drag in your prefab to the open slot and then set the number of times you want it to be created, you can play the game and see that you will have some new gameobjects in your scene that are turned off. This is the behaviour that we want. For me, this was my player lasers.

The above is where the magic is happening. This is the method that we will be calling whenever we want to turn on a gameobject for use.

First in the for loop we’re checking whether the gameobject is active in the hierarchy. If it is then we return and move onto the next object. Once finished we then return the gameobjects to the method called. If no objects are available we simply return.

For me, I’m going to need a lot of lasers further down the line. I have set the number to 10 but I will need more when the player gets further in the game. That is where the shouldExtend variable comes into play. If this is ticked in the Unity editor, then the object pool for this object can be extended automatically.

Now, the interesting part of the pooling system. Go into your script where you instantiate your gameobject. I am in my player laser script.

In the image above you can see some commented out code, that is the original behaviour of instantiating my lasers. It is a little more code for the new functionality but it is worth it.

First we get the gameobject from the pool, which I have called laser. I then wrap the code in a null check. I set the position and rotation of the laser, as you would in the instantiation.
Next you need to set the gameobject active. I then follow up with adding some force to my laser. You won’t need that for your game object.

The main parts you will need from the code above are:

  1. Get the object from the pool Gameobject laser
  2. set the position
  3. set the rotation
  4. set the object ACTIVE

And that is it for the first part of using object pooling.

If you found this helpful, don’t forget to clap and follow for more articles on game development in Unity.

--

--