Create an object pool in Unity part 2
Objective: Extend the object pool so that we can use multiple objects in the pool
I am going to assume that you have been through the previous article about object pooling. If you haven’t read it I advise you take a quick look as this article will continue on from where it finished.
You should have an object pool system that currently works with one gameobject and you can pool as many of them as you like. Now, we’re going to extend the functionality to allow for multiple gameobjects to be pooled.
First you need to open the ObjectPool script and begin by moving three variables. Before moving the variables though, on top of the ObjectPool class but underneath the libraries, you will need to create a new class.
By making the new class System.serializable we can now make the instances of this class editable when using the Unity editor. Once the new class has been made we need to move the three variables mentioned into the new class as these are the variables that we want to be different on each instance.
The three variables to move are also the same ones that have been commented out in the ObjectPool class. Now, in the ObjectPool class you will need to create a new list of the new class and call it itemsToPool. This will create an editable list in the Unity editor that will allow us to determine how many different objects we want to pool .
You can see above that I have 5 different objects that I want to pool and how many of each that I want. You can also tell the system which ones you will want to expand on.
Now that we have the new class setup, we need to make a few changes to the start method and the GetPooledObject methods. Let’s start with Start().
This method is very similar to what we had before but with the added foreach loop to go through each item in the new list of type ObjectPoolItem. I have also added in a transform.parent, with this I am setting the objects to be created as a child to an empty gameobject in the scene. This allows for the hierarchy to be kept organised.
Now that we have changed the Start method it is time to move onto pulling out the objects we want.
The method is now slightly longer and more complicated than before and that is because we need to be able to pick out the pooled objects we want. Say, the player needs a laser, well I need to be able to pick out the player laser from the pool instead of the turretlaser. We will do this by passing in a string of the tag name for that object.
In the first for loop we’re checking to make sure that there are available objects to pull out of the pool and which object we want, by comparing the tag name and the string passed into the method.
Here on the left you can see where I am calling to get an object from the pool with the tag of EnemyShip1, you can also see the prefab for that object which has the same tag name. This means that when I come to call this method and spawn a ship, it will look in the pool for that particular ship. If you have multiple ships like I do, you can make this string a [serializable] field and change the string in the inspector which would make the spawn script more agile for different ships, but that is not the point here. Moving on.
The next loop is a foreach loop which goes through the pool with the tag name and if the object can be expanded then more objects will be created and turned to the off state, ready to be used. If none of the above are run, then the method will return null.
The only thing left for you to do is to add in the tag name for the objects when you’re turning them to the on state and to add more objects to your pool.
If you found this helpful, don’t forget to clap and follow for more articles on game development in Unity.
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