# 1. ObjectPool Fundamentals Part of [[Object Pooling and Spawners]]. Next: [[2. Projectile Pools]]. > [!warning] Code not compiled in Unity > Described from the official 6000.5 `ObjectPool<T>` Scripting Reference and that Editor's CoreModule XML (see the hub). Not configured in a scene. ## 1.1 The official type ```csharp using UnityEngine.Pool; var pool = new ObjectPool<MyItem>( createFunc: () => new MyItem(), actionOnGet: item => item.OnGet(), actionOnRelease: item => item.OnRelease(), actionOnDestroy: item => item.Dispose(), collectionCheck: true, defaultCapacity: 10, maxSize: 10000); ``` `T` must be a **class**. Official constructor parameters (6000.5): | Parameter | Official role | |---|---| | `createFunc` | Called to create a new instance when the pool is empty. | | `actionOnGet` | Called when an instance is taken from the pool — set up and activate. | | `actionOnRelease` | Called when it is returned — clean up and deactivate. | | `actionOnDestroy` | Called when an instance **cannot** be returned because the pool is at `maxSize`. | | `collectionCheck` | If true, releasing an instance already in the pool throws. **Editor only, not in Player builds.** Default `true`. Leave it on while you write the game. | | `defaultCapacity` | Starting *capacity of the internal stack*, default 10. Official remarks: this does **not** create that many instances at construction. Instances are created **one at a time** on `Get`. | | `maxSize` | Maximum instances the pool will **hold**, default 10000. Official ctor: further returns are "ignored and can be garbage collected." Official `Release`: the instance "is destroyed." Official `actionOnDestroy` is that destroy path. For GameObjects, **you** must `Destroy` in `actionOnDestroy`. | `Get()` returns `T`, creating via `createFunc` if the stack is empty. `Release(T)` returns it to the pool (or destroy-at-max). `Get(out T item)` returns a `PooledObject<T>` whose `Dispose` releases the item: ```csharp using (pool.Get(out var item)) { item.DoWork(); } // Release is called here ``` Use that form for short-lived *managed* objects (part 5). For projectiles that outlive the spawning method, keep the `Get()` / `Release()` pair on the component. Counts (official): `CountActive` (created and not yet returned), `CountInactive` (currently in the pool), `CountAll` (active + inactive). `Clear()` and `Dispose()` remove pooled items and invoke the destroy callback for each item **in the pool**. Call `Dispose()` from `OnDestroy` on the MonoBehaviour that owns the pool. Official constraint, worth repeating: **`UnityEngine.Pool` is not thread-safe.** Do not `Get`/`Release` from a Job. ## 1.2 Prewarm Because `defaultCapacity` does not instantiate, a first wave of `Get` can spike Instantiate. If you care about that first frame, prewarm yourself: ```csharp for (var i = 0; i < prewarmCount; i++) { var item = pool.Get(); pool.Release(item); } ``` Clamp `prewarmCount` to `maxSize`. Do this in `Awake` or during a loading screen, not mid-combat. ## 1.3 LinkedPool, GenericPool, UnsafeGenericPool - **`LinkedPool<T>`**: official "linked list version of `IObjectPool<T>`." Same create/get/release/destroy callbacks and Editor collection checks, plus a `maxSize`, but **no** `defaultCapacity`. Use it when you do not need a pre-sized stack. - **`GenericPool<T>`**: official "static implementation of `ObjectPool<T>`" for types you would otherwise `new`. Convenient for plain C# classes. Not how you pool a prefab. - **`UnsafeGenericPool<T>`**: the same idea without collection checks. Prefer the checked pool until you have measured a reason not to. This series uses `ObjectPool<T>` for scene objects and `ListPool<T>` for temporary collections (part 5). Next: [[2. Projectile Pools]].