using System.Collections.Generic; using UnityEngine; public class BoxObjectPool : MonoBehaviour { public static BoxObjectPool current; [Tooltip("Assign the box prefab.")] public Indicator pooledObject; [Tooltip("Initial pooled amount.")] public int pooledAmount = 1; [Tooltip("Should the pooled amount increase.")] public bool willGrow = true; List pooledObjects; void Awake() { current = this; } void Start() { pooledObjects = new List(); for (int i = 0; i < pooledAmount; i++) { Indicator box = Instantiate(pooledObject); box.transform.SetParent(transform, false); box.Activate(false); pooledObjects.Add(box); } } /// /// Gets pooled objects from the pool. /// /// public Indicator GetPooledObject() { for (int i = 0; i < pooledObjects.Count; i++) { if (!pooledObjects[i].Active) { return pooledObjects[i]; } } if (willGrow) { Indicator box = Instantiate(pooledObject); box.transform.SetParent(transform, false); box.Activate(false); pooledObjects.Add(box); return box; } return null; } /// /// Deactive all the objects in the pool. /// public void DeactivateAllPooledObjects() { foreach (Indicator box in pooledObjects) { box.Activate(false); } } }