-1

I am trying to use an object pool to keep track of and spawn enemies in a round based survival game in unity that I started for a class last semester. Its been about 6 months since I looked at the project and I am not very experienced with using stacks and dictionaries so I am having trouble diagnosing the problem. I've been fiddling around with stuff for about a week now but have not been able to figure out the problem so I was wondering if someone on here could help me out.

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

public class poolerScript : MonoBehaviour
{
    [System.Serializable]
    public class pool
    {
        public string tag;
        public GameObject prefab;
        public int size;
    }

    EntityHealth EntityHealth;
    public static poolerScript Instance;

    private void Awake()
    {
        Instance = this;
    }

    Stack<GameObject> inactive;
    public List<pool> pools;
    public Dictionary<string, Queue<GameObject>> poolDictionary;

    // Start is called before the first frame update
    void Start()
    {
        poolDictionary = new Dictionary<string, Queue<GameObject>>();

        foreach (pool pool in pools)
        {
            Queue<GameObject> objectPool = new Queue<GameObject>();

            for (int i = 0; i < pool.size; i++)
            {
                GameObject obj = Instantiate(pool.prefab);
                obj.SetActive(false);
                objectPool.Enqueue(obj);
            }

            poolDictionary.Add(pool.tag, objectPool);
        }
    }


    public GameObject spawnFromPool(string tag, Vector3 position, Quaternion rotation)
    {
        if (!poolDictionary.ContainsKey(tag))
        {
            Debug.LogWarning("Pool with tag " + tag + " does not exist");
            return null;
        }

        GameObject objectToSpawn = poolDictionary[tag].Dequeue();

        objectToSpawn.SetActive(true);
        objectToSpawn.transform.position = position;
        objectToSpawn.transform.rotation = rotation;

        iPooledObj pooledObj = objectToSpawn.GetComponent<iPooledObj>();

        if (pooledObj != null)
        {
            pooledObj.onObjectSpawn();
        }

        poolDictionary[tag].Enqueue(objectToSpawn);

        return objectToSpawn;
    }

    public void Despawn(GameObject obj)
    {
        obj.SetActive(false);

        Debug.Log($"Adding a(n) {obj.name} back to the pool.");
        inactive.Push(obj);
    }
}

the problem I am having is in the very last line of code inactive.Push(obj); the first 2 lines in the method are being triggered and in the console I get a message saying my enemy is being added back to the pool but then a null reference exception also shows up in the console and new enemies are not spawned. Any help would be appreciated

1 Answers1

0

inactive is declared but never assigned in the code provided.

In the Start method I suggest adding inactive = new Stack<GameObject>();

Jeffrey Parks
  • 554
  • 4
  • 14