I usually declare the list at the start of my class and then define it in the Awake() function, but that’s up to you. You can also just put all of that directly in your class.
To do it like I do:
public List<GameObject> enemies;
void Awake()
{
enemies = new List<GameObject>();
}
In the Editor you probably won’t even notice a difference between arrays and lists. It’s just that they handle slighlty differently in code.
With lists you normally want to loop through its elements if you plan to do something with multiple elements. I’ll tell you how I’d do it, but keep in mind that there are a lot of different ways to do this and none are strictly ‘better’.
Assuming the enemies are already in the list, I’d then write a generic ‘EnemyTurn’ method that handles the turn of the enemy you pass into it:
private IEnumerator EnemyTurn(Enemy enemy)
{
//handle the turn
}
This gets called for each enemy in the list by another Coroutine, that waits for the previous one to finish:
foreach (GameObject enemy in enemies)
{
StartCoroutine(EnemyTurn(enemy.GetComponent<Enemy>()));
yield return new WaitForSeconds(2f);
}
I think you can also do something like
yield return (StartCoroutine(EnemyTurn()));
and it might wait until it’s done, but I’m not sure.
If an enemy dies you delete it from the list with enemies.Remove(), so it doesn’t get called by the foreach loop anymore. So now you can have as many enemies as you like, from 0 to 1000000 if you want, and don’t have to add additional ones manually.
To give the enemies different attacking times (which seems to be like you want to do that) or actually any other different behavior, you just give the enemies different variable values and get them in the EnemyTurn() Coroutine:
IEnumerator EnemyTurn(Enemy enemy)
{
float attackWait = enemy.GetAttackWait();
yield return new WaitForSeconds(attackWait);
}
By the way - you should call Coroutines with their method instead of the name (EnemyTurn() instead of “EnemyTurn”). It’s a bit more efficient.