I have got a script called ObjectPooler, that is currently taking a prefab of two types of enemies and then pooling then at runtime. In another script which I have called a spawn manager, it sets the prefabs active and places them in the game world in waves.
void SpawnEnemyWave(int enemiesToSpawn)
{
for (int i = 0; i < enemiesToSpawn; i++)
{
GameObject enemy1 = ObjectPooler.SharedInstance.GetPooledPrefab("Basic Enemy");
if (enemy1 != null)
{
enemy1.transform.position = GenerateSpawnPosition();
enemy1.SetActive(true);
}
}
for (int i = 0; i < enemiesToSpawn; i++)
{
GameObject enemy2 = ObjectPooler.SharedInstance.GetPooledPrefab("Fast Enemy");
if (enemy2 != null)
{
enemy2.transform.position = GenerateSpawnPosition();
enemy2.SetActive(true);
}
}
Is it possible to put the GameObjects enemy1 and enemy2 into a GameObject Array or List called enemy in this case, then have a variable such as randomEnemy = Random.Range(0, enemy.Length) and then set enemy list/array as active. To activate randomly either of the two types of enemy?
Why?! Do you need to use pooling? It makes everything perhaps 3x or 4x as complex and is unlikely to ever give you much benefit unless you have 1000+ or more of these things and constantly spawn/destroy them (like bullets).
The costs and issues associated with object pooling / pools:
Are these returned items prefabs? Or are they actual instances in scene?
If they’re truly prefabs that you must now Instantiate yourself, then the above will work.
If they are already Instantiated, you are going to have both of them in the scene, so I suppose you could choose to randomly destroy them but that might have other issues.
So they are prefabs that have been cloned and then appear in the game scene. So in wave one just one enemy should be set active, then the second wave there should be two enemies active etc. There should be random chance though of which type of enemy is set active each time. Currently both enemy types are set active in the first wave, in the second wave two types of both enemy etc. Then once the enemies fall off the game platform I set active to false.
I know that I want something similar to this:
GameObject[] enemyPrefab;
void SpawnEnemyWave(int enemiesToSpawn)
{
for(int i = 0; i < enemiesToSpawn; i++)
{
int randomEnemy = Random.Range(0, enemyPrefab.Length);
Instantiate(enemyPrefab[randomEnemy], GenerateSpwanPosition(), enemyPrefab[randomEnemy].transform.rotation);
}
}
But because I have got the separate scripts, I think this is where I’m getting confused. The SpawnManager script is where I’m doing the logic for the waves, and setting the pooled objects active, and Object Pooler is self explanatory.
I did it that way though as I thought it wouldn’t be too dissimilar to just instantiating two objects in an array/list. With the above code GameObject[ ] enemyPrefab, Enemy is placed in element 0 and Fast Enemy is in element 1. Then the length of that object can be determined and then one can be randomly selected. For object pooling though I can’t do the same? They have to be separate instances, even if they have been assigned a different tag within the editor? Would it be easier to handle object pooling and activating them in the same script?
You can do it any way you like but the best system designs make small granular systems and larger systems built on top of them… so:
each pooler handles ONE type of thing
you may have multiple poolers
to choose randomly you have a list (perhaps with an odds table as well) of possible poolers to use.
Honestly this is exactly why object pooling feels so filthy and dirty and yucky. Object pooling leaves a miserable destructive stain all over your project that will have to be dealt with endlessly into the future, endless technical debt, a vastly-increased surface over where errors can occur, it’s just a complete nightmare.
I have deleted SO many object pooling systems when I have gotten called in to firefight a project that was struggling with them. And without exception, EVERY time I have deleted a borked object pooler, the performance didn’t meaningfully change. Now with that code gone, no more bugs!