Wouldn’t keeping an enemy count static variable be more efficient than looping through all the Position objects and checking their children each frame ?
The static variable would just increase or decrease based on enemy being created or destroyed and then we can just spawn a fresh bunch when the count reaches 0.
1 Like
Probably, as it was the way they did in the previous game. I think the looping is just to teach this is possible. Who knows, maybe someone else’s future game needs to loop on all children of a game object for a non-trivial action, and then they’ll remember this lesson.
1 Like
Yea, i think that’s the case
EnemyFormation
void SpawnEnemies(){
foreach (Transform child in transform)
{
GameObject enemy = Instantiate(enemyPrefab, child.transform.position, Quaternion.identity) as GameObject;
enemy.transform.parent = child;
enemiesCount++;
}
}
public void enemyKilled(){
Debug.Log("One down!");
enemiesCount--;
if(enemiesCount == 0){
Debug.Log("Oh no, again!");
SpawnEnemies();
}
}
ENEMY
void OnTriggerEnter2D(Collider2D collider){
Projectile laserShot = collider.gameObject.GetComponent<Projectile>();
if (laserShot){
health -= laserShot.GetDamage();
laserShot.Hit();
if (health <= 0.0f) {
GetComponentInParent<EnemySpawner>().enemyKilled();
Destroy(gameObject);
}
}
}
Maybe this is what are you talking about 
This definitely makes more sense to me!
Both ways are correct, in the other hand… his way is going to be used later on the course. So i think you should keep working like he is doing here, you wont find any trouble following him ![]()