Finding inactive game objects in a pool

In order to avoid instantiate/destroy at runtime, I built a simple object pool that contains enemy scripts (all enemy classes are inherited from the same abstract class, so I can create a pool that holds all types of enemy in one list).

However, I don’t know how to find them and reference them correctly. Here’s what I did:

	public List<GameObject> GetEnemy (int amount, string type) {
		//amount: how many enemies are needed
		//type: what type of enemy is needed
		List<GameObject> go = new List<GameObject>();
		//end ForEach loop if got enough enemy
		//the Enemy.cs had a public string that indicates the type of enemy
		enemyRef.ForEach(script => {if (go.Count > amount) return; else if( (!script.gameObject.activeInHierarchy) && (script.type == type) ) go.Add(script.gameObject);} );
		return go;
	}

Although it works, it allocates about 1 kb memory and cause a 2.5 ~ 3ms spike for 800 game objects. This doesn’t really kill the frame rate, but there must be a better way.

Is there any better way to find and reference a collection of game objects under certain conditions?

i just created an object pool here it is if you like.

public class ObjectPool : MonoBehaviour {
	List<List<GameObject>> GameObjectPools;
	List<GameObject> GameObjectPool;
	GameObject Temp;
	public int POOL_SIZE;
	public int GROWTH_SIZE;

	int Counter;
	// Use this for initialization
	void Start () {
		Counter = 0;
		GameObjectPools = new List<List<GameObject>> ();

	}

void FixedUpdate()
{

}
public GameObject RetrieveObject(GameObject BaseObject)
{
	foreach(List<GameObject> Item in GameObjectPools)
	{
		Temp = Item[0];
		if((Temp.name == (BaseObject.name + "(Clone)") || Temp.name == BaseObject.name) && Item.Count > 1)
		{
			Item.RemoveAt(0);
			Temp.SetActive(true);
			Counter++;
			return Temp;
		}
	}
	throw new System.NullReferenceException("RetrieveObject(BaseObject)  BaseObject is not pooled, null error");
}
public bool PoolObject(GameObject BaseObject)
{
	foreach(List<GameObject> Item in GameObjectPools)
	{
		Temp = Item[0];
		if(Temp.name == BaseObject.name)
		{
			BaseObject.SetActive(false);
			Item.Add(BaseObject);
			return true;
		}
	}
	throw new System.NullReferenceException("PoolObject(BaseObject)  BaseObject is not pooled, null error");
	return false;
}
IEnumerator PrePoolHelper(List<GameObject> GameObjectPool, GameObject BaseObject)
{
	for (int counter = 0; counter < POOL_SIZE; counter++)
	{
		Temp = Instantiate(BaseObject) as GameObject;
		Temp.SetActive(false);
		GameObjectPool.Add(Temp);
		if(counter % GROWTH_SIZE == 0)
		{
			yield return new WaitForEndOfFrame();
		}
	}
	GameObjectPools.Add(GameObjectPool);
}
public void PrePool(GameObject BaseObject)
{
	GameObjectPool = new List<GameObject>();

	StartCoroutine(PrePoolHelper(GameObjectPool, BaseObject));
}

}

the way it works basically you supply it with how many of each object you want to pool
and how many you want to add per frame (this is basically for pre pooling so if you want 1,000 items you can set it to 10 and it’ll spread out the pooling over a 100 frames to help prevent you from trashing your framerate with 1,000 objects being pooled at once, unity will also actually throw up errors if you try to instantiate thousands of objects in a single frame)

when you want to use it in game

you do

ObjectPool PoolComponent = new PoolComponent();

foreach(gameobject object in poolist)
{
PoolComponent.prepool(object);
}

when you want to instantiate a new object wtihout using instantiate

//its set up to work with clone word attached or not to the object name

PoolComponent.retrieveobject(object);

to send it back

PoolComponent.poolobject(object);

stores it again

the key i’d say with my thing is that i have a list of lists

each object you want to pool gets its own list so for example if you have 10 objects and 1,000 clones each

it only searches the first item in each of the 10 lists to see if it can fidn the object to pool it doesn’t iterate through all 10,000 doing a comparison.

checking one object in a list ensures all the clones are removed from any chance at being looked at.