Game Dev Cheat Sheet

Unity Object Pool Generator

Generate production-ready Unity object pooling code with configurable pool size, auto-expand, callbacks, and more.

Object pooling is essential for performance in Unity. Instead of Instantiate and Destroy (which trigger garbage collection), you pre-create objects and reuse them. This generator outputs a complete pooling class based on your configuration.

Configuration

Pool type
Auto-expand when empty
Optional features

Generated Code

Uses UnityEngine.Pool.ObjectPool<T>, available in Unity 2021.1 and later.

using UnityEngine;
using UnityEngine.Pool;

public class ProjectilePool : MonoBehaviour
{
    [SerializeField] private GameObject prefab;
    [SerializeField] private int initialSize = 20;

    private ObjectPool<GameObject> pool;

    private void Awake()
    {
        pool = new ObjectPool<GameObject>(
            createFunc: () => Instantiate(prefab),
            actionOnGet: obj => obj.SetActive(true),
            actionOnRelease: obj => obj.SetActive(false),
            actionOnDestroy: obj => Destroy(obj),
            defaultCapacity: 20,
            maxSize: 100
        );

        // Pre-warm the pool
        var prewarm = new GameObject[initialSize];
        for (int i = 0; i < initialSize; i++)
        {
            prewarm[i] = pool.Get();
        }
        for (int i = 0; i < initialSize; i++)
        {
            pool.Release(prewarm[i]);
        }
    }

    public GameObject Get()
    {
        GameObject obj = pool.Get();
        return obj;
    }

    public GameObject Get(Vector3 position, Quaternion rotation)
    {
        GameObject obj = Get();
        obj.transform.SetPositionAndRotation(position, rotation);
        return obj;
    }

    public void Release(GameObject obj)
    {
        pool.Release(obj);
    }
}

Usage Example

How to use the generated pool in your game scripts.

// Getting an object from the pool
[SerializeField] private ProjectilePool pool;

GameObject projectile = pool.Get(transform.position, transform.rotation);

// Returning it when done
pool.Release(projectile);

UnityEngine.Pool.ObjectPool vs custom pool

Unity 2021.1 added a built-in object pool. Here is when each approach is the right call.

UnityEngine.Pool.ObjectPoolCustom pool
Available sinceUnity 2021.1Any version
SetupGeneric API, simpleMore code, more flexibility
Inspector visibilityNonePossible with serialised fields
Cross-scene sharingNoYes if you build it
Best forBullets, particles, simple casesSingletons, scene-shared pools

Frequently asked questions

When should I use object pooling in Unity?
Pool any prefab you instantiate or destroy frequently: bullets, enemies, particles, UI popups, damage numbers. Instantiate and Destroy both allocate and trigger garbage collection. Pooling reuses existing instances and is essentially free per spawn.
Should I use Unity ObjectPool or a custom pool?
UnityEngine.Pool.ObjectPool, added in Unity 2021, is well designed and covers most cases. Use it unless you need pool sharing across scenes, custom warm-up behaviour, or pool inspection in the Inspector. Roll your own only when the built-in API genuinely cannot do what you need.
How big should my object pool be?
Start with the maximum number you expect to be active at once, plus 10-20% headroom. Profile during the busiest gameplay moment and adjust. Pools that are too small thrash; pools that are too large waste memory.
How do I reset a pooled object before reuse?
Implement a Reset or OnSpawn method on the pooled component, called from the pool action when an object is returned or taken. Reset transform, velocity, animator state, particle systems, and any custom fields. Forgetting to reset state is the most common pooling bug.

Last updated: