I have a simple click to move game. I need to add enemies in at the start of the game and then maintain that amount of enemies throughout the game, so when an enemy dies, it respawns where it started.
note: I have already set up a prefab in the editor, all I need is scripting support
Thanks
If i were you I’d create a class to store your enemy spawn details (You can add things more easily this way). Something simple like this:
public class Enemy {
public Enemy (Vector3 pos) {
position = pos;
prefab = Resources.Load ("PrefabRoute");
}
public Vector3 position;
public GameObject prefab;
public bool isDead; //Set to true if enemy dies
//More data you want/need
}
And then in an empty GameObject you would need to add a Monobehavior with a list of SpawnEnemies.
Note: The List is so that you dont care how many enemies will you spawn, it is WAY simpler and efficient.
using System.Collections.Generic; //Necessary to use lists
public class EmptyClass : Monobehaviour {
public List<Enemy> myEnemies;
public void Start () {
myEnemies.Add ( new Enemy (/*Position and other desired data*/));
//Add all your enemies here. You can have some randomness too
foreach (Enemy enemy in myEnemies) {
Spawn (enemy); //Initially spawn ALL your enemies
}
}
public void Update () {
foreach (Enemy enemy in myEnemies) {
if (enemy.isDead) {
Spawn (enemy); //Spawn if dead. You can add all sorts of delays and other stuff
}
}
}
public void Spawn (Enemy enemy) {
Instanciate (enemy.prefab, enemy.position, Quaternion.identity); //I'm not sure if this is correct, but it is a simple line to write.
}
}
I believe this is the most orginized, elegant and efficient way to do this. Hope it helps and that it was clear. Any doubt, dont mind asking 