using System.Collections; using System.Collections.Generic; using UnityEngine; public class ObjectPooler : MonoBehaviour { public GameObject prefab; public int pooledAmount = 20; public bool willGrow = true; private List pooledObjects; void Start () { pooledObjects = new List(); for (int i = 0; i < pooledAmount; i++) { GameObject obj = (GameObject)Instantiate(prefab); obj.SetActive(false); pooledObjects.Add(obj); } } public GameObject GetPooledObject() { for (int i = 0; i < pooledObjects.Count; i++) { if (!pooledObjects[i].activeInHierarchy) { return pooledObjects[i]; } } if (willGrow) { GameObject obj = (GameObject)Instantiate(prefab); pooledObjects.Add(obj); return obj; } return null; } }