ObjectPool problem

EDIT: Count() is actually the amount of deactivated objects. Not active objects.
Thanks here for the developers who wrote this code.


Hi,

I found this ObjectPool Code.
Website: http://unitypatterns.com/resource/objectpool/
Github ObjectPool Script: https://github.com/UnityPatterns/ObjectPool/blob/master/Assets/ObjectPool/Scripts/ObjectPool.cs

When I try this:
Debug.Log(ObjectPool.Count(cube));
I don’t get a number. It’s always zero.

My code:

// a prefab cube in prefab folder
public Transform cube;

void Start () {
    ObjectPool.CreatePool(cube);
    StartCoroutine (Cubes ());
}

IEnumerator Cubes ()
{
    while (true)
    {
        for (int i = 0; i < 10; i++)
        {
            Quaternion spawnRot = Quaternion.identity;


            ObjectPool.Spawn(cube, spawnPos.position, spawnRot);


            yield return new WaitForSeconds (spawnWait);
        }
       
        yield return new WaitForSeconds (waveWait);
    }
}

void Update () {
    Debug.Log (ObjectPool.Count(cube));
}

In the console I see only 0 (zero).

Has somebody an idea why?

Thanks.

So I found it odd that the Instantiate was delayed until the first spawn call.

When I created my Pooling system I instantiate on start up a set number of Prefabs/GameObjects. Then it would pull from that list of items, use the item and add it back when its finished.

I’d honestly have to test it myself to see how it worked completely.

If your still stuck and want a pooling system I can offer you mine. I built a custom editor for it. You can specify a parent which is very handy for keeping your scene clean or applying a specific position (GUI elements) and number to create on start up. Obviously for free. Just give me a private message.