- Home /
How can I prevent active gameobjects from enqueueing to my object pool?
So I've been fiddling around with my object pool as I've noticed that objects that stay on the screen too long deactivate and reactive which is problematic since the projectiles should be only be disabled by the players interaction or if they hit the health object. I have a general idea of where I'm supposed to fix it but I'm not sure how to type it. This is what I got so far.
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class ArrowSpawner : MonoBehaviour { [System.Serializable] public class Pool { public string tag; public GameObject prefab; public int size; }
public Transform[] SpawnPoints;
public GameObject[] ArrowReflector;
public static int arrowDetectSpawn;
public static Vector3 arrowDetectObj;
public static GameObject OriginatedFrom;
public static int arrowNumber = 0;
public GameObject[] Arrow;
int randomSpawnPoint, randomArrow;
public bool spawnAllowed;
public List<Pool> pools;
public Dictionary<string, Queue<GameObject>> poolDictionary;
public static List<GameObject> arrowList = new List<GameObject>();
public float spawnSpeed = 1f;
public float spawnStart = 0f;
public static GameObject obj = null;
private Queue<GameObject> temp;
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);
obj.name = "arrow" + arrowNumber;
arrowNumber++;
arrowList.Add(obj);
}
poolDictionary.Add(pool.tag, objectPool);
}
if(spawnAllowed == true)
{
InvokeRepeating("SpawnArrows", spawnStart, spawnSpeed);
}
}
void Awake()
{
var arrowFind = GameObject.FindWithTag("Arrow");
if (arrowFind)
{
Destroy(Arrow[randomArrow]);
}
}
public void Update()
{
/* for (int i = 0; i < 10; i++)
{
Debug.Log(arrowList[i]);
}*/
}
public GameObject SpawnFromPool (string tag, int position, Quaternion rotation)
{
GameObject objectToSpawn = poolDictionary[tag].Dequeue();
objectToSpawn.SetActive(true);
randomSpawnPoint = Random.Range(0, SpawnPoints.Length);
objectToSpawn.transform.position = SpawnPoints[randomSpawnPoint].transform.position;
objectToSpawn.transform.rotation = rotation;
arrowDetectSpawn = randomSpawnPoint;
arrowDetectObj = objectToSpawn.transform.position;
/*
if (object in poolDictionary.isActive)
{
Don't Enqueue said object.
}
else
{
Lets add it to the queue.
}
*/
poolDictionary[tag].Enqueue(objectToSpawn);
return objectToSpawn;
}
void SpawnArrows()
{
if (spawnAllowed)
{
SpawnFromPool("Arrow", randomSpawnPoint, Quaternion.identity);
}
}
}
Any help would be much appreciated.
I'm having a similar issue after using Brackeys' youtube tutorial. Did you find a solution? I like the use of a tag vs. other list-based pooling options that use indexes to reference the pooled objects and would like to figure it out too.
Your answer
Follow this Question
Related Questions
Key Combo system. where I am going wrong with this? 2 Answers
Object Pooling vs Particle System 1 Answer
Object Pooling Help 1 Answer
Choose 10 items on each iteration 1 Answer
Switch-case with enum 1 Answer