0

I am trying to make a simple object pool in c# for Unity. So far I have used this class:

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class ObjectPool : MonoBehaviour
{

    public static ObjectPool ShareInstance;
    public List<GameObject> pooledObjects;
    public GameObject objectToPool;
    public int amountToPool;

    // Use this for initialization
    void Start()
    {
        pooledObjects = new List<GameObject>();

        for (int i = 0; i < amountToPool; i++)
        {
            GameObject obj = (GameObject)Instantiate(objectToPool);
            obj.SetActive(false);
            pooledObjects.Add(obj);
        }
    }

    void Awake()
    {
        ShareInstance = this;
    }

    public GameObject GetPooledObject()
    {
        for (int i = 0; i < pooledObjects.Count; i++)
        {
            if (!pooledObjects[i].activeInHierarchy)
            {
                return pooledObjects[i];
            }
        }
        return null;
    }
}

The start method makes the inactive instances of my prefab correctly. However I am finding an error when I use this code to call GetPooledObject:

    GameObject optionTile = ObjecdtPooler.ShareInstance.GetPooledObject();
    if (optionTile != null)
    {
        optionTile.SetActive(true);
    }

The GetPooledObject method appears to think that the list (pooledObjects) is empty. This means that the for loop within this method is just skipped, and null is returned. I used debug.log and the list is correctly filled with the instances of my prefab. However when I call the GetPooledObject method, the list is said to be empty.

Any ideas?

Thanks

ryeMoss
  • 4,303
  • 1
  • 15
  • 34
hinch
  • 91
  • 2
  • 12
  • Is the optionTile requested in another Start method? Then it would be possible that the request is executed before ObjectPool.Start() is called. – Lelefant Feb 02 '18 at 15:11
  • @Lelefant No, optionTile is used nowhere else. Thanks for the suggestion. – hinch Feb 02 '18 at 15:31
  • 1
    I mean your second code snippet, when is it called? After all Start() methods? The order, in which different Start() methods are called is not defined. If the second snippet lies in another Start() method, it might be called before the pool is initialized. – Lelefant Feb 02 '18 at 15:34
  • I think that is it! I can't check it right now but it is something that I didn't think about. Thanks. – hinch Feb 02 '18 at 15:37

1 Answers1

0

I expect that either amountToPool has a inital value of 0 or if it's greater than 0 all objects are in use. So you should expand your pool to grow by a given value when all objects are in use.

user743414
  • 936
  • 10
  • 23
  • Hi @user743414 Sorry i forgot to mention this in my description but i have already set the amount to pool, and the instances are being made. All of the instances are inactive which means that they should be chosen. However this stil doesnt explain why the list.count is 0 – hinch Feb 02 '18 at 15:05
  • @hinch Then the only answer could be that not the pool is used which you're expecting. – user743414 Feb 02 '18 at 15:10