- Home /
Object Pooling Help
I've just started creating a Object Pooler for my game to be able to recycle multiple things such as explosion effects and enemies. I followed a tutorial online, this one: https://www.youtube.com/watch?v=dm_r8KxeS7Y and ended up with this script:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class ObjectPooler : MonoBehaviour {
public static ObjectPooler current;
public GameObject pooledObject;
public int pooledAmount;
List<GameObject> pooledObjects;
void Start()
{
pooledObjects = new List<GameObject>();
for(int i = 0; i < pooledAmount; i++)
{
GameObject obj = (GameObject)Instantiate(pooledObject);
pooledObjects.Add(obj);
obj.SetActive(false);
}
}
public GameObject InstantiatePooledObject()
{
for(int i = 0; i < pooledObjects.Count; i++)
{
if(!pooledObjects[i].activeInHierarchy)
{
return pooledObjects[i];
}
}
GameObject obj = Instantiate(pooledObject) as GameObject;
obj.SetActive(false);
pooledObjects.Add(obj);
return obj;
}
public void DestroyPooledObject(GameObject obj, float time)
{
StartCoroutine(DestoyingPooledObject(obj, time));
}
IEnumerator DestoyingPooledObject(GameObject obj, float time)
{
yield return new WaitForSeconds(time);
pooledObjects.Add(obj);
obj.SetActive(false);
yield break;
}
}
I did change a couple of things to suit my needs including adding a method that will disable a pooled object after a certain amount of time. Then I simply attached it to a empty GameObject and filled in the empty variables. This Object Pooler works fine but the main big problem with it is that it only pools one object and when I want to use the Object Pooler for enemies, it won't work the way it is since I'm already using it for explosion effects.
I was wondering if someone could help me get this working for multiple GameObjects? I can't really think of a way to do it. And sorry if this is an amateur question.
Answer by KuR5 · Aug 03, 2016 at 05:30 AM
Hey,
What you did is just fine and you can use it for your requirement as below.
Use this script multiple times for multiple objects like enemy and explosion effect. Further you can not use Singleton (instance named current). You can reference all of them at another manager class and by using references from that you can access all pools.
Ex.
Lets Make gameobjects named EnemyPool and ExplosionPool and assign this script with respected values to each of them. Now you can access EnemyPool using GameObject.Find("EnemyPool").GetComponent<ObjectPooler>()
and ExplosionPool using GameObject.Find("ExplosionPool").GetComponent<ObjectPooler>()
I hope this will help you, and free to ask further help you need.
I don't know why I didn't think of doing that :P. Thanks :)
Another issue I have though is in my game, I have rounds and I'm spawning different enemies each round which I of course want to be recycled but I don't see a way of setting up an EnemyPool that will work for lots of different enemies
Your answer
![](https://koobas.hobune.stream/wayback/20220612090050im_/https://answers.unity.com/themes/thub/images/avi.jpg)