Game Dev Cheat Sheet

Unity Object Pool Generator

Generate a Unity pooling class with configurable capacity, optional callbacks and timed returns.

Object pooling reuses instances to reduce repeated creation, destruction and allocation costs. Generate a starting implementation, then profile it with your prefabs and spawning patterns.

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;
using System.Collections.Generic;

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)
    {
        if (obj == null) return;
        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?
Consider pooling frequently spawned bullets, enemies, particles or UI elements when profiling shows creation, destruction or allocation costs. Reusing an instance still has activation, reset and callback costs, and retaining a pool uses memory.
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: