# 2. Projectile Pools
Part of [[Object Pooling and Spawners]]. Back: [[1. ObjectPool Fundamentals]]. Next: [[3. Particle and VFX Pools]].
> [!warning] Code not compiled in Unity
> `PooledProjectile` and `ProjectilePool` were type-checked against Unity 6000.5.8f1 (`ObjectPool<T>`, `Rigidbody.linearVelocity`, `OnCollisionEnter`). They have not been play-tested in a scene.
## 2.1 What must be reset
A reused projectile is not a new one. If you only `SetActive(true)`, the last shot's **`Rigidbody.linearVelocity`** and **`angularVelocity`** (official 6000.5 physics properties) are still there. Reset them on spawn and again on despawn. Parent the inactive instance back under a `poolRoot` so it does not ride a moving shooter out of the world.
`createFunc` is the only place that should `Instantiate` the prefab. `actionOnDestroy` is the only place that should `Destroy` it. Everything in between is Get/Release.
## 2.2 PooledProjectile
The projectile holds a reference to its pool and releases itself when lifetime elapses or a collision happens. It does not `Destroy(gameObject)`.
```csharp
using UnityEngine;
/// <summary>
/// A projectile that returns itself to an ObjectPool on lifetime end or impact.
/// Reset physics here so a reused body does not keep the last shot's velocity.
/// </summary>
public sealed class PooledProjectile : MonoBehaviour
{
[SerializeField] float lifetimeSeconds = 3f;
[SerializeField] Rigidbody body;
ProjectilePool _pool;
float _dieAt;
bool _inPool;
public void AttachToPool(ProjectilePool pool) => _pool = pool;
public void OnSpawned(Vector3 position, Quaternion rotation, Vector3 linearVelocity)
{
_inPool = false;
transform.SetPositionAndRotation(position, rotation);
if (body == null)
body = GetComponent<Rigidbody>();
if (body != null)
{
body.linearVelocity = linearVelocity;
body.angularVelocity = Vector3.zero;
}
_dieAt = Time.time + lifetimeSeconds;
}
public void OnDespawned()
{
_inPool = true;
if (body != null)
{
body.linearVelocity = Vector3.zero;
body.angularVelocity = Vector3.zero;
}
}
void Update()
{
if (_inPool)
return;
if (Time.time >= _dieAt)
ReleaseToPool();
}
void OnCollisionEnter(Collision collision)
{
if (_inPool)
return;
ReleaseToPool();
}
public void ReleaseToPool()
{
if (_inPool || _pool == null)
return;
_pool.Release(this);
}
}
```
`Time.time` is scaled time. If you later pause the game with `timeScale = 0` ([[Menus, Pause and Settings Flow]]), either stop the projectiles or drive lifetime with `Time.unscaledTime`.
## 2.3 ProjectilePool
```csharp
using UnityEngine;
using UnityEngine.Pool;
/// <summary>
/// Official UnityEngine.Pool.ObjectPool wrapper for a projectile prefab.
/// createFunc Instantiates; actionOnDestroy Destroys so a maxSize overflow
/// does not leak GameObjects.
/// </summary>
public sealed class ProjectilePool : MonoBehaviour
{
[SerializeField] PooledProjectile prefab;
[SerializeField] Transform poolRoot;
[SerializeField] int defaultCapacity = 16;
[SerializeField] int maxSize = 64;
[SerializeField] bool collectionCheck = true;
[SerializeField] int prewarmCount;
ObjectPool<PooledProjectile> _pool;
public int CountActive => _pool != null ? _pool.CountActive : 0;
public int CountInactive => _pool != null ? _pool.CountInactive : 0;
public int CountAll => _pool != null ? _pool.CountAll : 0;
void Awake()
{
if (prefab == null)
{
Debug.LogError("ProjectilePool: assign a PooledProjectile prefab.", this);
return;
}
_pool = new ObjectPool<PooledProjectile>(
createFunc: Create,
actionOnGet: OnGet,
actionOnRelease: OnRelease,
actionOnDestroy: OnDestroyItem,
collectionCheck: collectionCheck,
defaultCapacity: defaultCapacity,
maxSize: maxSize);
var warm = Mathf.Clamp(prewarmCount, 0, maxSize);
for (var i = 0; i < warm; i++)
Release(CreateAndGetForWarmup());
}
PooledProjectile Create()
{
var instance = Instantiate(prefab, poolRoot);
instance.AttachToPool(this);
instance.gameObject.SetActive(false);
return instance;
}
PooledProjectile CreateAndGetForWarmup()
{
return _pool.Get();
}
void OnGet(PooledProjectile projectile)
{
projectile.gameObject.SetActive(true);
}
void OnRelease(PooledProjectile projectile)
{
projectile.OnDespawned();
projectile.gameObject.SetActive(false);
if (poolRoot != null)
projectile.transform.SetParent(poolRoot, false);
}
void OnDestroyItem(PooledProjectile projectile)
{
if (projectile != null)
Destroy(projectile.gameObject);
}
public PooledProjectile Get(Vector3 position, Quaternion rotation, Vector3 linearVelocity)
{
var projectile = _pool.Get();
projectile.OnSpawned(position, rotation, linearVelocity);
return projectile;
}
public void Release(PooledProjectile projectile)
{
if (projectile == null || _pool == null)
return;
_pool.Release(projectile);
}
void OnDestroy()
{
_pool?.Dispose();
_pool = null;
}
}
```
`Get` from a weapon script:
```csharp
pool.Get(muzzle.position, muzzle.rotation, muzzle.forward * muzzleSpeed);
```
`prewarmCount` runs `Get`/`Release` in `Awake` because official `defaultCapacity` does not instantiate. Keep `collectionCheck` on in the Editor until double-release bugs are gone (part 5).
If a projectile can outlive its weapon, that is fine: the pool, not the weapon, owns the instance.
Next: [[3. Particle and VFX Pools]].