using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class PoolManager : MonoBehaviour {

	public GameObject []template;
	private List<GameObject> pool = new List<GameObject>();

	// Use this for initialization
	void Start () {
	
	}

	public GameObject GetObject()
	{
		if (pool.Count == 0) {
			return CreateNewObjectInPool();
		}
		
		foreach (GameObject child in pool) {
			if(!child.activeSelf)
			{
				child.SetActive(true);
				return child;
			}
		}
		
		return CreateNewObjectInPool();
	}

	GameObject CreateNewObjectInPool()
	{
		GameObject temp = Instantiate(template[Random.Range(0, template.Length)]) as GameObject;
		pool.Add (temp);
		return temp;
	}
}
