I have a question regarding spawning an array of GameObjects in Unity. What I want to happen is that when a big enemy dies, it will spawn enemies into their respective Transform positions (i.e., Enemy 1 will spawn in Position 1, Enemy 2 in position 2, and so on).
I suppose I could go on with something like:
// C#
public GameObject[] Enemies;
public Transform[] SpawnPoints;
void KillEnemy()
{
GameObject enemy1 = Instantiate(Enemies[0], SpawnPoints[0].position, SpawnPoints[0].rotation;
GameObject enemy2 = Instantiate(Enemies[1], SpawnPoints[1].position, SpawnPoints[1].rotation;
GameObject enemy3 = Instantiate(Enemies[2], SpawnPoints[2].position, SpawnPoints[2].rotation;
Destroy(gameObject)
}
But the problems are:
1. I'm aware that the code is expensive to run, especially since I'm doing a 2D game; and
2. It provides no flexibility, meaning if I want a different enemy to use the same code, then I won't be able to do stuff like adding more enemies to spawn (after the first one dies, of course).
So yeah, I hope I get some help with this. Thanks!
for
loop? \$\endgroup\$for
loop but I'm not sure how to implement it. Anyway, I might as well give it a try! \$\endgroup\$