/// <summary> /// Headstarts a pool and grows it. /// </summary> /// <param name="Size">Number of objects to instantiate</param> public void InstantiatePool(int Size) { //Debug.Log("Instantiating pool for , " + ObjectName + "," + Size + "Times"); //What if there is no original object here if (Original == null) { Debug.LogError("Cannot Instantiate Pool, Original is empty"); } for (int i = 0; i < Size; i++) { //make a new Object From Original GameObject ToAdd = Instantiate(Original); //deactivate object for storage ToAdd.SetActive(false); //Add PoolMember Script PoolMember pm = ToAdd.AddComponent <PoolMember>(); pm.MyPool = this; //Trigger Event pm.OnPoolEnter(); //Add to Pool Objects.Add(ToAdd); } }
/// <summary> /// Adds An object to pool /// </summary> /// <param name="ToGive">Object to add to the pool. ATTENTION! it must be like original, or else...</param> public void AddNew(GameObject ToGive) { //Deactivate object for storage ToGive.SetActive(false); //Add PoolMember Script if there is NOTT PoolMember pm = ToGive.GetComponent <PoolMember>(); if (pm == null) { pm = ToGive.AddComponent <PoolMember>(); //and if we fail to add PoolMember to object. <EPIC-FAIL> if (pm == null) { Debug.LogError("There is a problem on Pool.AddNew, We cant add PoolMember to it. ''Sorry man. I just cant...''"); } } //set pool for new PoolMember pm.MyPool = this; //Trigger On Pool Enter. We want it to be deactivated and ready to be pooled pm.OnPoolEnter(); //And Add to pool as usual Objects.Add(ToGive); }