My code below code works fine, but the issue is, I use this exact same code layout for 6 more pooled items in my game (bullets, enemies etc) So my manager class contains a lot of repetitive variables and code like I have above, is there a more efficient way I can portray this code? Maybe using a generic class and making a call when needed or something similar?
#EDIT
A better example of what I mean, I have crystals, enemies and ammo, all being pooled at the beginning, set active and being stored in a parent, I am doing it for each object. Is the a better way to optimize this so I only need to use it once?
//
//Objects to spawn
//
//The object to be spawned
public GameObject[] crystal;
public GameObject[] swarmEnemy;
//The object to be spawned
public GameObject[] ammo;
//
//Lists
//
//The list which holds all the crystals
public List<GameObject> crystals;
public List<GameObject> swarmEnemies;
//The list which holds all the ammo
public List<GameObject> ammoList;
//
//Parents
//
public GameObject crystalParent;
public GameObject swarmEnemyParent;
public GameObject ammoListParent;
void Start()
{
crystals = new List<GameObject>();
swarmEnemies = new List<GameObject>();
ammoList = new List<GameObject>();
for (int i = 0; i < pooledAmount; i++)
{
GameObject crystal1 = (GameObject)Instantiate(crystal[0]);
GameObject crystal2 = (GameObject)Instantiate(crystal[1]);
GameObject crystal3 = (GameObject)Instantiate(crystal[2]);
GameObject crystal4 = (GameObject)Instantiate(crystal[3]);
crystals.Add(crystal1);
crystals.Add(crystal2);
crystals.Add(crystal3);
crystals.Add(crystal4);
for (int j = 0; j < crystals.Count; j++ )
{
crystals[j].SetActive(false);
crystals[j].transform.parent = crystalParent.transform;
}
GameObject enemy1 = (GameObject)Instantiate(swarmEnemy[0]);
GameObject enemy2 = (GameObject)Instantiate(swarmEnemy[1]);
swarmEnemies.Add(enemy1);
swarmEnemies.Add(enemy2);
for (int j = 0; j < swarmEnemies.Count; j++)
{
swarmEnemies[j].SetActive(false);
swarmEnemies[j].transform.parent = swarmEnemyParent.transform;
}
GameObject ammo1 = (GameObject)Instantiate(ammo[0]);
GameObject ammo2 = (GameObject)Instantiate(ammo[1]);
ammoList.Add(ammo1);
ammoList.Add(ammo2);
for (int j = 0; j < ammoList.Count; j++)
{
ammoList[j].SetActive(false);
ammoList[j].transform.parent = ammoListParent.transform;
}
}
}