Sitemap

Object Pooling Design Pattern in Unity3d.

6 min readOct 24, 2024

Objective: Object Pooling Design Pattern in Unity3d.

Object Pooling Design pattern, is the design pattern that use a technique to create a list of objects once then give object to player when request it, when object used it returned to a pool and not destroyed.

This give ability to re-use these object to boost performance of application avoiding creation and destroying object which is performance heavy especially for mobile application.

Create three scripts Player, Pooling Manager and Bullet.

Designing Object pooling.

Go to Pooling Manager script and make this script a singleton to make easy accessing it .

Press enter or click to view image in full size

We need an object which will be used, this can be a laser , bullet etc. I will create a variable of Game Object called bullet Prefab.Go to editor create bullet prefab the drag and drop it to the inspector.

Press enter or click to view image in full size

A bullet will need to go to the list since will need to have many of them for re-using them. So we need to create a list of Game Object, we need to able to dynamic increase the size of this list , so avoid using array since its fixed sized can nob be increased its size at run time.

Press enter or click to view image in full size

Create a method called Generate Bullets which will take int parameter.

Press enter or click to view image in full size

Before generate any bullet we need to know how many bullet we need to create, to do this we need to iterate according to the size of our list.

Note: I find if I use list.count for condition logic my editor freeze , am not sure why but I think it may be am using old editor 2018 , lol don't judge me I like it it very fast for my old pc.

Press enter or click to view image in full size

Every time we iterate , we need to create a bullet and store the value to a handle to be able to use this object. After create this bullet we need to set this bullet in Active and add it to the List.

Press enter or click to view image in full size

We need to make sure when the game start we create this bullets, so call this method when the game start. This method take the amount of bullets we need to generate.

If you play the editor you will see a bullet generated in bad order.

Now we need to clean our hierarchy, create a empty game object inside in the Pooling Manger Game Object. Called Bullet Container.

Go to the Pooling Manager script and create a variable of type game object and go back to the editor drag and drop that Bullet Container to the Inspector.

Press enter or click to view image in full size
Press enter or click to view image in full size

Now we need to parent our bullets to this Bullets Container, we do this inside Generate Bullets method then after create a bullet we need to set new bullet transform parent’s to bullets container, we do this using newBullet.transform.parent and assign it to the bullets container transform.

Press enter or click to view image in full size

Now we need to Create another method this will be for requesting a bullet, but this time we need to know what type of method needed, when some one request thing we will need to return the same type of thing was requested.

So this method need to return a game object since our bullet is game Object , also this method need to be public since will be called by player(Other script if its friendly AI).

In Request Bullet Method before request bullet we need to Iterate through a list.

Press enter or click to view image in full size

After iterate we need to check if the object in a list is not active We do this using Api called GameObject.ActiveInHierarchy, then we set the bullet active.

Press enter or click to view image in full size

After turn game object to active state, we return the bullet using keyword return.

Press enter or click to view image in full size

Hey! what will happen if all object in the list are current used?

In that state we will need to create extra bullet to give player, so we need to do this outside the loop since all object are active in this assumed scenario.

Create a local variable of type Game Object called extraBullet, then assign this handle to Instantiated prefab.

Press enter or click to view image in full size

Now extra Bullet needs to go to the list also need to be inside the bullet Container.

Press enter or click to view image in full size

The next step is to return the extra bullet .

Press enter or click to view image in full size

Now go to the player script and call the Request method.

Press enter or click to view image in full size

If we try to play the editor you will find the bullet are not re-use instead we will create new bullet when all object are active.

Press enter or click to view image in full size

Now go to the bullet script create a method and set the bullet active state to false. Then call this method on Enable, by doing this we will make our bullet to deactivate after some.

Note: The logic of disabling the bullet depend on your self, you may want to disable the object if it hit wall, enemy or reached certain distance so customize Hide to your need and you may need to call it OnTriggerEnter to detect collision.

Press enter or click to view image in full size

Now I can not even pass the 7 bullet the player use the same bullet to shoot many enemies. If player finger are too fast, extra bullet will be created on a fly boom.

Press enter or click to view image in full size

See you into the next one.

--

--

Suleiman Abdullah
Suleiman Abdullah

Written by Suleiman Abdullah

My name is Suleiman Abdullah. I learn game development and share what I learn by writing articles. :https://ko-fi.com/suleimanabdullah

No responses yet