Eonevolve Learning
Unity and DevelopmentUnity and codeIntermediate~7 min

Object pooling in Unity with explicit state

Snapshot

~85 sec

Object pooling reuses temporary GameObjects through an explicit acquire, use, reset, and return lifecycle. The important skill is not memorizing a queue, but keeping ownership and reset behavior visible.

You will learn

  • Trace one generic GameObject through the full pool lifecycle.
  • Connect C# lines to changes in the available and in-use collections.
  • Explain why reset belongs inside the release transition.

Target outcome

You can describe and inspect a small Unity pool whose state changes are explicit and repeatable.

Visual walkthrough

Visual walkthrough

~7 min

  1. The pool lifecycle in four concepts

    • Available

      Inactive objects that have been reset and can be acquired.

    • In use

      Objects temporarily owned by the caller and tracked by the pool.

    • Acquire

      Moves one object into use, creating a generic instance only when none is ready.

    • Release

      Ends temporary ownership, resets state, deactivates the object, and returns it.

  2. Acquire, use, reset, return

    Trace one generic object through a closed pool cycle. Reset belongs inside release, not after the next caller already inherited stale state.

    1. Ready

      Inactive object waits in the available queue.

      available.Enqueue(item);
    2. Acquire

      Pool hands out temporary ownership and activates the object.

      available.Dequeue(); inUse.Add(item);
    3. Reset

      Release clears temporary state before the object can return.

      item.transform.SetPositionAndRotation(...);
    4. Return

      Object deactivates and becomes available again.

      item.SetActive(false);

    reusesReturnReady

    Trace one generic object through a closed pool cycle. Reset belongs inside release, not after the next caller already inherited stale state.

    Reading order

    1. Ready

      Inactive object waits in the available queue.

      available.Enqueue(item);
    2. Acquire

      Pool hands out temporary ownership and activates the object.

      available.Dequeue(); inUse.Add(item);
    3. Reset

      Release clears temporary state before the object can return.

      item.transform.SetPositionAndRotation(...);
    4. Return

      Object deactivates and becomes available again.

      item.SetActive(false);

    Connection explanations

    1. Ready → Acquire (request)

      Acquire moves one object from ready into temporary ownership.

    2. Acquire → Reset (release)

      Release must verify ownership and clear temporary state before reuse.

    3. Reset → Return (enqueue)

      Only a reset object should re-enter the available queue.

    4. Return → Ready (reuses)

      Return closes the cycle: the same object is ready for a later acquire without a fresh Instantiate.

  3. Make ownership changes explicit in C#

    This intentionally small MonoBehaviour shows the state boundary. Production code may add capacity and lifecycle policies, but those details are not needed to understand the transition.

    Languagecsharp
    public sealed class ReusablePool : MonoBehaviour
    {
        [SerializeField] private GameObject template;
        private readonly Queue<GameObject> available = new();
        private readonly HashSet<GameObject> inUse = new();
    
        public GameObject Acquire()
        {
            var item = available.Count > 0
                ? available.Dequeue()
                : Instantiate(template);
            item.SetActive(true);
            inUse.Add(item);
            return item;
        }
    
        public void Release(GameObject item)
        {
            if (!inUse.Remove(item)) return;
            item.transform.SetPositionAndRotation(Vector3.zero, Quaternion.identity);
            item.SetActive(false);
            available.Enqueue(item);
        }
    }

    Code annotations

    1. Lines 4-5

      Keep two explicit collections

      The queue represents ready objects and the set represents temporary ownership.

      Effect: An object cannot be understood as both ready and in use at the same moment.

    2. Lines 7-15

      Acquire and track

      The pool reuses a ready object or creates a generic fallback, then activates and records it.

      Effect: The available count can decrease while the in-use count increases.

    3. Lines 17-24

      Reset before return

      Release first verifies ownership, then clears transform state, deactivates, and enqueues.

      Effect: The next caller receives an inactive object from a known baseline.

  4. Check ownership before reuse

    Why does Release remove the object from inUse before adding it to available?

    Expected reasoning

    The removal proves that the pool currently owns the release transition. Only then can reset and enqueue move the object back to the available state without duplicating ownership.

Complete state sequence

Step through one pooled object

Every state and transition is listed below. The optional controls only change emphasis; they do not add teaching information.

Interactive controls become available when this section loads.

Current state

Ready

The object waits inactive in the available queue.

Observable output
available contains the object; inUse does not.

Next transition

Ready to Acquired

Input
Acquire request
Effect
Dequeue, activate, and add to inUse.
Output
Temporary ownership begins.

Complete state sequence

  1. Step 1 of 4

    Ready

    The object waits inactive in the available queue.

    Observable output
    available contains the object; inUse does not.
  2. Step 2 of 4

    Acquired

    Acquire removes and activates the object for temporary ownership.

    Observable output
    The object is active and appears in inUse.
  3. Step 3 of 4

    Reset

    Release verifies ownership and clears temporary transform state.

    Observable output
    Temporary state is back at the declared baseline.
  4. Step 4 of 4

    Ready again

    The object is deactivated and returned to the queue.

    Observable output
    The object is inactive and available for another request.

Complete transition sequence

  1. Ready to Acquired

    Input
    Acquire request
    Effect
    Dequeue, activate, and add to inUse.
    Output
    Temporary ownership begins.
  2. Acquired to Reset

    Input
    Release request
    Effect
    Remove from inUse and reset temporary state.
    Output
    The object reaches a known baseline.
  3. Reset to Ready again

    Input
    Reset completed
    Effect
    Deactivate and enqueue.
    Output
    The object can be acquired again.