- Home /
Most efficient way to create/spawn a lot of enemies at the same time(Unity3D)
I googled a lot and most EnemySpawners I found online only spawn one Enemy after another. In my game I want to spawn hordes of enemies in timed waves. So I did it the best way I could think of, which is the code snippet below. Around the code snippet there is a system that checks if it is time to spawn enemies. The "yield return new WaitForEndOfFrame();" is to make sure, that not every enemy is spawning at the same time, so the game is not freezing because of it, it is still very slow though. Is there a more efficient way? The enemies are prefabs that I instantiate and they have a Skinned Mesh Renderer. If you need more Information, I am happy to provide them. Thanks in advance
float currentSpawnZ = ...;
foreach (EnemySpawn enemySpawn in nextSpawnGroup.enemySpawns) {
List<GameObject> enemyList = new List<GameObject>();
float currentSpawnX = ...;
for (int i = 0; i < enemySpawn.amount; i++)
{
GameObject enemy = Instantiate(enemySpawn.enemy, hierarchyParent, true);
enemy.transform.position = new Vector3(currentSpawnX, ... , currentSpawnZ);
enemyList.Add(enemy);
currentSpawnX += spawnXGap;
yield return new WaitForEndOfFrame();
}
currentSpawnZ += spawnZGap;
enemiesSpawnedEvent.Invoke(enemyList);
}
Answer by Vega4Life · Aug 12, 2019 at 05:57 PM
Seems like pooling could be a route you could take. https://learn.unity.com/tutorial/object-pooling
Create your spawns, hide them, then when you need to show a huge horde.. just unhide them. As they die, just put them back in the pool, reset them, and unhide them again till you need them.
Your answer
![](https://koobas.hobune.stream/wayback/20220612210534im_/https://answers.unity.com/themes/thub/images/avi.jpg)