← Back to list

Tower Defense — Improving the Object Pool

So, I recently told a small fib. My technical coach was holding an open session, and I stated that I had the missiles and explosions being…

Sean Duggan · 2025-06-15 19:02 · 99 claps · 1.5 min read
#unity #tower-defense #object-pooling
Open on Medium ↗
Wiki topics: 🎮 · Gaming

Tower Defense — Improving the Object Pool

So, I recently told a small fib. My technical coach was holding an open session, and I stated that I had the missiles and explosions being grabbed out of my object pool. I had not, but thought it would be quick enough to amend. It wasn’t.

OK, as per this article, I have a generic object pool set up. I had an instance of it added to my SpawnManager, and grabbed it as one of the components. But, well, each missile turret ought to be drawing from the same object pool. It doesn’t make much sense to have them pulling from the SpawnManager, so my first impulse was to use the Singleton pattern so there is one and only one instance that everyone uses.

public static ObjectPoolManager Instance { get; private set; }
private void Awake()
{
    if (Instance != null)
    {
        Debug.LogError("There's more than one ObjectPoolManager! " + transform + " - " + Instance);
        Destroy(gameObject);
        return;
    }
    Instance = this;
}

We’ll have our GameManager set up the pools for missiles and explosions.

ObjectPoolManager.Instance.CreatePool(_explosionPrefab, 30, 100);
ObjectPoolManager.Instance.CreatePool(_missilePrefab, 30, 100);

Then, on the MissileLauncher and Missile, we’ll just pull from that.

GameObject rocket = ObjectPoolManager.Instance.GetFromPool(_missilePrefab); //instantiate a rocket
GameObject explosion = ObjectPoolManager.Instance.GetFromPool(_explosionPrefab);

Except… it doesn’t work. No missiles get fired, and after adding some Debug statements, it’s for a simple reason, no such pool exists. Why? Well, my current setup for the pools assumes we’re fetching by prefab, I have two different downloaded missile launchers that have their own missile prefab, and I accidentally mixed the two when defining the Missile for the GameManager, and for the Missile Launcher

Which… yeah, one of those cases of being a bit too clever. I should be indexing my dictionary via something easier to track like a string or integer value. Probably the easiest thing to do is to have another setup involving tuples where the user can specify the identifier, prefab, and the pool values, and it will initialize those pools at the outset, then when I retrieve items, I just use the identifier.


메타데이터
post_id
0c85abfd4c0e
slug
tower-defense-improving-the-object-pool-0c85abfd4c0e
url
https://medium.com/@sean.duggan/tower-defense-improving-the-object-pool-0c85abfd4c0e
canonical_url
https://medium.com/@sean.duggan/tower-defense-improving-the-object-pool-0c85abfd4c0e
author_url
https://medium.com/@sean.duggan
status
ok
fetched_at
2026-08-10 11:41:09