DEV Community

Cover image for Unity Object Pooling: A Complete Guide to Better Game Performance
Alok Krishali
Alok Krishali

Posted on Edited on

Unity Object Pooling: A Complete Guide to Better Game Performance

Unity Object Pooling is a performance optimization technique in Unity that helps reduce unnecessary object creation and destruction during gameplay. Instead of repeatedly creating new GameObjects with Instantiate() and removing them with Destroy(), you can create objects in advance and reuse them whenever needed.

For example, bullets, enemies, coins, explosions, and particle effects are frequently created and destroyed in many games. When hundreds of these objects are generated during gameplay, repeated instantiation and destruction can introduce performance overhead and garbage collection pressure.

This is where object pooling becomes useful. In my Unity development experience, reusing existing GameObjects is a practical way to manage frequently spawned objects more efficiently.

In this tutorial, you'll learn how Unity Object Pooling works, how to create a simple object pool using C#, and how to reuse GameObjects with a practical bullet example. I'll also explain when to use object pooling and how to reset pooled objects correctly.

What Is Unity Object Pooling?

Unity Object Pooling is a technique where we create objects once and reuse them instead of constantly creating and destroying them.

For example, without pooling, a shooting game may work like this:

Shoot
 ↓
Instantiate Bullet
 ↓
Bullet Hits Enemy
 ↓
Destroy Bullet
Enter fullscreen mode Exit fullscreen mode

This happens again every time the player shoots.

With object pooling, the process becomes:

Create Bullets
 ↓
Store Them in Pool
 ↓
Get Bullet
 ↓
Use Bullet
 ↓
Return Bullet to Pool
 ↓
Reuse Bullet
Enter fullscreen mode Exit fullscreen mode

An object pool usually maintains a collection of inactive GameObjects. When the game needs an object, it retrieves one from the collection and activates it. Once the object is no longer needed, it is deactivated and returned to the pool for future use.

The main idea is simple: reuse objects instead of creating new ones every time.

Why Use Object Pooling in Unity?

Object pooling is useful when your game creates and removes the same objects frequently.

Some common examples are:

  • Bullets
  • Enemies
  • Coins
  • Explosions
  • Particle effects
  • Projectiles
  • Damage numbers

It can reduce the amount of repeated Instantiate() and Destroy() calls and help avoid unnecessary performance overhead, especially when many objects are involved.

Benefits of Unity Object Pooling

1. Reduces repeated object instantiation.
Calling Instantiate() repeatedly—especially for bullets, enemies, or particle effects—takes CPU time and can cause frame drops or stutters. Pooling creates objects ahead of time (or on demand) and simply activates/deactivates them when needed.

2. Helps reduce garbage collection pressure.
Constantly creating and destroying objects generates memory allocations and dead references that Unity’s garbage collector eventually has to clean up. Reusing pooled objects keeps memory usage steadier and avoids sudden GC spikes during gameplay.

3. Improves management of frequently spawned objects.
Instead of scattered Instantiate() and Destroy() calls across scripts, a pool gives you a single place to control active counts, reset object state (position, health, timers), and cap maximum instances so performance stays predictable.

How to Use Object Pool in Unity

Let's create a simple object pool for bullets.

Step 1: Create a Bullet Prefab

First, create your bullet GameObject and add the components you need.

Bullet and Projectile Prefab

For example:

Bullet
 ├── Sprite Renderer
 ├── Collider2D
 └── Rigidbody2D
Enter fullscreen mode Exit fullscreen mode

Once the bullet is ready, drag it from the Hierarchy into your Project folder to create a prefab.

We will use this prefab to create our pool.

Step 2: Create the Object Pool Script

Create a C# script called ObjectPool.

Add the following code:

using System.Collections.Generic;
using UnityEngine;

public class ObjectPool : MonoBehaviour
{
    public GameObject objectPrefab;
    public int poolSize = 30;

    private Queue<GameObject> objectPool = new Queue<GameObject>();

    void Start()
    {
        for (int i = 0; i < poolSize; i++)
        {
            GameObject obj = Instantiate(objectPrefab);

            obj.SetActive(false);

            objectPool.Enqueue(obj);
        }
    }

    public GameObject GetObject()
    {
        if (objectPool.Count > 0)
        {
            GameObject obj = objectPool.Dequeue();

            obj.SetActive(true);

            return obj;
        }

        return null;
    }

    public void ReturnObject(GameObject obj)
    {
        obj.SetActive(false);

        objectPool.Enqueue(obj);
    }
}
Enter fullscreen mode Exit fullscreen mode

Don't worry if the code looks a little new. The idea is quite simple.

We create a number of objects when the game starts and keep them inside a Queue.

For example:

public int poolSize = 30;
Enter fullscreen mode Exit fullscreen mode

creates a pool with 30 objects.

These objects are initially disabled:

obj.SetActive(false);
Enter fullscreen mode Exit fullscreen mode

They are now ready to be reused.

Step 3: Create the Pool in Unity

Create an empty GameObject in your scene and rename it:

BulletPool
Enter fullscreen mode Exit fullscreen mode

Attach the ObjectPool script to it.

In the Inspector, you will see:

Object Prefab
Pool Size
Enter fullscreen mode Exit fullscreen mode

Drag your Bullet prefab into Object Prefab and set the pool size to something like 30.

When you press Play, Unity will create the bullets and keep them disabled.

Step 4: Get an Object From the Pool

Now we can use the pool when the player shoots.

For example:

public ObjectPool bulletPool;

void Shoot()
{
    GameObject bullet = bulletPool.GetObject();

    if (bullet != null)
    {
        bullet.transform.position = transform.position;
      // Also can set rotation and Velocity to Zero Here
    }
}
Enter fullscreen mode Exit fullscreen mode

Instead of creating a new bullet with:

Instantiate(bulletPrefab);
Enter fullscreen mode Exit fullscreen mode

we get an existing bullet from the pool:

bulletPool.GetObject();
Enter fullscreen mode Exit fullscreen mode

The bullet is then activated and can be used normally.

Step 5: Return the Object to the Pool

Return the Bullet to the Pool after Hit
When bullet Hit Enemies In a shooting game, bullets should be returned to the object pool when they hit an enemy instead of being destroyed using Destroy(). We can use Unity's OnTriggerEnter2D() method to detect collisions and return the bullet to the pool.

For example:

using UnityEngine;

public class Bullet : MonoBehaviour
{
    private ObjectPool objectPool;

    public void SetPool(ObjectPool pool)
    {
        objectPool = pool;
    }

    private void OnTriggerEnter2D(Collider2D other)
    {
        if (other.CompareTag("Enemy"))
        {
            objectPool.ReturnObject(gameObject);
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

In this example:

  1. OnTriggerEnter2D() detects when the bullet enters another 2D collider.
  2. CompareTag("Enemy") checks whether the bullet has hit an enemy.
  3. ReturnObject(gameObject) deactivates the bullet and returns it to the pool for future reuse.

Important: Make sure the bullet's Collider2D has Is Trigger enabled, the enemy has the appropriate Collider2D, and at least one object involved in the collision has a Rigidbody2D.

Return Bullets When They Leave the Screen
Sometimes bullets don't hit any enemy and simply travel outside the camera view. Keeping these bullets active wastes available pooled objects.

You can use OnBecameInvisible() to return bullets when they are no longer visible to any camera:

private void OnBecameInvisible()
{
    if (gameObject.activeInHierarchy)
    {
        objectPool.ReturnObject(gameObject);
    }
}
Enter fullscreen mode Exit fullscreen mode

This method is useful for simple 2D shooting games. However, remember that OnBecameInvisible() is based on camera visibility, not actual screen boundaries.

For more precise control, you can check the bullet's position against the camera's viewport bounds.

Important: Avoid returning a bullet multiple times. Your ReturnObject() method should check whether the object is already inactive or already inside the pool before adding it back.

When the bullet hits an enemy or leaves the screen, we don't destroy it.

Instead, we return it to the pool:

bulletPool.ReturnObject(gameObject);
Enter fullscreen mode Exit fullscreen mode

The object is disabled and placed back into the pool.

Later, when another bullet is needed, the same GameObject can be reused.

This is the main benefit of Unity Object Pooling.

Don't Forget to Reset the Object

There is one important thing to remember.

A reused object may still have its previous state.

For example, a bullet may still have its old:

  • Position
  • Rotation
  • Rigidbody velocity
  • Animation state
  • Health or other values

So make sure you reset anything that needs to be reset when the object is reused.

For a Rigidbody2D bullet, for example:

Rigidbody2D rb = bullet.GetComponent<Rigidbody2D>();

rb.Velocity = Vector2.zero;
rb.angularVelocity = 0f;
Enter fullscreen mode Exit fullscreen mode

Then set its new position and direction.

Unity's Built-in Object Pooling API

Unity provides a built-in object pooling API through the UnityEngine.Pool namespace. Instead of creating your own pooling system using a Queue or List, you can use the ObjectPool<T> class to manage reusable objects more efficiently.

The built-in ObjectPool<T> API is available in modern Unity versions, including Unity 2021 LTS and later. It provides methods to retrieve objects, return them to the pool, and control how objects are created and reused.

How to Use Unity's Built-in Object Pooling

Let's create a simple bullet pooling system using Unity's built-in ObjectPool<GameObject> class.

Step 1: Import the Required Namespace

First, import the Unity pooling namespace:

using UnityEngine;
using UnityEngine.Pool;
Enter fullscreen mode Exit fullscreen mode

Step 2: Create the Object Pool

Create a script called BulletPool.cs and add the following code:

using UnityEngine;
using UnityEngine.Pool;

public class BulletPool : MonoBehaviour
{
    [SerializeField] private GameObject bulletPrefab;
    [SerializeField] private int defaultCapacity = 10;
    [SerializeField] private int maxSize = 50;

    private ObjectPool<GameObject> bulletPool;

    private void Awake()
    {
        bulletPool = new ObjectPool<GameObject>(
            CreateBullet,
            OnGetBullet,
            OnReleaseBullet,
            OnDestroyBullet,
            false,
            defaultCapacity,
            maxSize
        );
    }

    private GameObject CreateBullet()
    {
        return Instantiate(bulletPrefab);
    }

    private void OnGetBullet(GameObject bullet)
    {
        bullet.SetActive(true);
    }

    private void OnReleaseBullet(GameObject bullet)
    {
        bullet.SetActive(false);
    }

    private void OnDestroyBullet(GameObject bullet)
    {
        Destroy(bullet);
    }

    public GameObject GetBullet()
    {
        return bulletPool.Get();
    }

    public void ReturnBullet(GameObject bullet)
    {
        bulletPool.Release(bullet);
    }
}
Enter fullscreen mode Exit fullscreen mode

Step 3: Spawn and Return Bullets

Now, you can retrieve bullets from the pool whenever the player shoots and return them when they are no longer needed.

For example, create a PlayerShoot.cs script:

using UnityEngine;

public class PlayerShoot : MonoBehaviour
{
    [SerializeField] private BulletPool bulletPool;
    [SerializeField] private Transform firePoint;

    public void Shoot()
    {
        GameObject bullet = bulletPool.GetBullet();

        bullet.transform.SetPositionAndRotation(
            firePoint.position,
            firePoint.rotation
        );
    }
}
Enter fullscreen mode Exit fullscreen mode

When a bullet hits an enemy or leaves the screen, call:

bulletPool.ReturnBullet(gameObject);
Enter fullscreen mode Exit fullscreen mode

This deactivates the bullet and makes it available for reuse.

Understanding the ObjectPool Methods

Here are the main methods and callbacks used in Unity's built-in pooling system:

Method / Callback Purpose
ObjectPool<T>() Creates a new object pool
Get() Retrieves an object from the pool
Release() Returns an object to the pool
Clear() Destroys objects currently stored in the pool
CountInactive Gets the number of inactive pooled objects
CountActive Gets the number of objects currently checked out
CountAll Gets the total number of objects created and not destroyed

The constructor also accepts callbacks such as actionOnGet, actionOnRelease, and actionOnDestroy, allowing you to control what happens when an object is retrieved, returned, or destroyed.

Custom Object Pool vs Unity's Built-in Object Pool

Both approaches help you reuse GameObjects, but there are some differences:

Feature Custom Object Pool Unity ObjectPool
Implementation Write your own logic Built-in API
Object creation Manual Callback-based
Object retrieval Custom method Get()
Object return Custom method Release()
Maximum size Implement manually Built-in option
Flexibility Full control Configurable callbacks

Which One Should You Use?

If you are learning object pooling or need a highly customized pooling system, creating your own implementation is a good way to understand how it works.

However, for most Unity projects, the built-in ObjectPool<T> class is a convenient choice because it provides ready-made pooling functionality with less code to maintain.

Keep in mind that Unity's built-in pool is not automatically thread-safe, and its maximum size controls how many inactive objects are retained, not the maximum number of active objects.

When Should You Use Object Pooling?

You don't need to use pooling for every GameObject.

It makes the most sense when an object is:

  • Created frequently
  • Destroyed frequently
  • Reused many times
  • Needed in large numbers

For example, a bullet in a shooting game is a great candidate for pooling.

On the other hand, objects such as a main menu, player character, or settings panel usually don't need an object pool because they are not repeatedly created and destroyed.

Final Thoughts

Unity Object Pooling is a simple but useful technique for games that frequently create and destroy objects.

Instead of doing this:

Instantiate → Use → Destroy
Enter fullscreen mode Exit fullscreen mode

we can do:

Get From Pool → Use → Return To Pool
Enter fullscreen mode Exit fullscreen mode

When I used this approach in my game, it made more sense to create the objects once and reuse them rather than repeatedly creating new ones.

If you're working on a shooting game, endless runner, survival game, or any project with lots of repeated objects, learning how to use object pool can be very useful.

Start with a simple pool like the one above. Once you understand the concept, you can move to Unity's built-in pooling system and create a more flexible solution.

Frequently Asked Questions

1. What is Unity Object Pooling?

Unity Object Pooling is a technique that reuses GameObjects instead of repeatedly creating and destroying them.

2. How do I use object pool in Unity?

Create a group of objects in advance, get an object when you need it, activate it, and return it to the pool when you are finished with it.

3. What is the Object Pooling Design Pattern?

The Object Pooling Design Pattern is a programming pattern that stores reusable objects so they can be used again instead of being recreated.

4. Does object pooling improve Unity performance?

It can improve performance when many short-lived objects are repeatedly created and destroyed. The actual benefit depends on your game's workload.

5. Which objects are good for object pooling?

Bullets, enemies, projectiles, coins, explosions, particle effects, and damage numbers are common examples.

Top comments (0)