# 5. Collection Pools, Tests and Pitfalls Part of [[Object Pooling and Spawners]]. Back: [[4. Enemy Spawners]]. > [!warning] Code not compiled in Unity > `TempListExample` was type-checked against Unity 6000.5.8f1 (`ListPool<T>.Get(out T)`). It has not been run in Play Mode. ## 5.1 Official collection pools `ListPool<T>` is officially "a version of `CollectionPool<TCollection, TItem>` for Lists." The same namespace has `DictionaryPool` and `HashSetPool`. Official `CollectionPool.Get(out TCollection)` returns a `PooledObject` that releases the collection when disposed — the same pattern as `ObjectPool.Get(out T)`. ```csharp using System.Collections.Generic; using UnityEngine; using UnityEngine.Pool; /// <summary> /// Official CollectionPool/ListPool pattern: Get(out list) returns a PooledObject /// that Releases the list when disposed. /// </summary> public static class TempListExample { public static int CountActiveChildren(Transform root) { if (root == null) return 0; using (ListPool<Transform>.Get(out var list)) { for (var i = 0; i < root.childCount; i++) list.Add(root.GetChild(i)); return list.Count; } } } ``` Do **not** return the list to a caller and then dispose it in the same method (the official 6000.5 CollectionPool example returns a list obtained with `Get()` and expects the *caller* to `Release`). The `using` form is for work that stays inside the method. Leaving a list in the pool without `Clear` is usually fine because official collection pools clear on return, but do not keep a leftover reference after `Release` or after the `using` ends. `GenericPool<T>` is the static pool for plain `new T()` classes (scratch buffers, message objects). It is not a prefab pool. ## 5.2 What was type-checked The pooling assembly built against Unity **6000.5.8f1** with no errors (CS0649 only) on 2026-11-30: `PooledProjectile`, `ProjectilePool`, `ParticleFxPool`, `PooledEnemy`, `EnemySpawner`, `TempListExample`. Nothing was play-tested in the Editor. ## 5.3 Symptom table | Symptom | Check first | |---|---| | Exception on `Release` in the Editor only | Double-release. Official `collectionCheck` throws in the Editor, not in Players. Find the second `Release` (death *and* `Destroy`, or a lifetime timer *and* a collision). | | Player build "leaks" what the Editor caught | Same bug; collection checks are off in Players. Keep checks on while developing. | | Reused projectile flies the old path | You did not zero `Rigidbody.linearVelocity` / `angularVelocity` ([[Physics 3D]]). | | Particle burst shows the previous explosion | Missing `Play()` (officially resets time to 0) or missing `StopEmittingAndClear` on Release. | | Inactive enemies still run AI / audio | `SetActive(false)` is the right first step; also stop NavMesh and audio in `OnDespawned` ([[2. Pooled AudioSource Service]]). | | GameObjects pile up after a chaotic fight | `maxSize` overflow without an `actionOnDestroy` that `Destroy`s the GameObject. Official overflow path will not clean a scene object for you. | | First wave hitch, later waves fine | `defaultCapacity` did not pre-instantiate. Prewarm, or Instantiates during a load screen. | | `Get` from a Job / thread crash or corruption | Official `UnityEngine.Pool` APIs are **not thread-safe**. Stay on the main thread. | | Pool still holds items after a scene unload | Call `Dispose()` in `OnDestroy`. `Clear`/`Dispose` only touch items *in* the pool; active ones must be Released first or they are orphaned. | | Audio still pooling separately | Keep the audio service in [[2. Pooled AudioSource Service]]; do not put AudioSources on every pooled bullet unless you measured a reason. | ## 5.4 What not to pool - Unique, long-lived objects (the player, a boss that exists once). - Anything you cannot reset cheaply. If reset costs more than Instantiate, the pool is theatre. - Background-thread or Job-created objects via `ObjectPool`. - UI Toolkit visual elements you already recycle with `ListView.makeItem` ([[3.HUD and Inventory Example]]) — that is a different reuse path. Back to the hub: [[Object Pooling and Spawners]].