Object pooler not recycling objects children, keeping them inactive but the parent object active.
Here is the script, any ideas and suggestions is greatly appreciated. Thanks
using UnityEngine; using System.Collections; using System.Collections.Generic;
public class ObjectPooler : MonoBehaviour {
public GameObject pooledObject;
public int pooledAmount;
List<GameObject> pooledObjects;
// Use this for initialization
void Start () {
pooledObjects = new List<GameObject> ();
for (int i = 0; i < pooledAmount; i++)
{
GameObject obj = (GameObject) Instantiate (pooledObject);
obj.SetActive (false);
pooledObjects.Add (obj);
}
}
public GameObject GetPooledObject()
{
for (int i = 0; i < pooledObjects.Count; i++)
{
if (!pooledObjects [i].activeInHierarchy)
{
return pooledObjects[i];
}
}
GameObject obj = (GameObject) Instantiate (pooledObject);
obj.SetActive (false);
pooledObjects.Add (obj);
return obj;
}
}
Comment
Your answer
Follow this Question
Related Questions
Instantiate GameObject Error I dont know whats wrong with it seems right to me 1 Answer
How to reuse prefab bullet when it hits a collider using pool 0 Answers
My pooling stops after a while , please help 0 Answers
Pooled Objects on wrong scene 0 Answers
Unity Compiler Error not all paths return a value - Object Pooling 1 Answer