The lesson involves creating a spawnDelay
so all the enemies don’t appear at the same time. This method is called inside Start
, which means that there is a delay until the entire formation is “filled” of number of enemies
* spawnDelay
. Imagining 5 enemies and a 2 second spawn delay, that means the initial population of the formation takes 5 * 2 = 10 seconds
. The way SpawnUntilFull
is written, it will fill all the positions until it’s executed when all positions are filled (appropriately named…).
Any enemies killed during the execution of SpawnUntilFull
don’t really count because they’re refilled and will continue to do so until all the enemies have been given a chance to spawn. At this point, killed enemies will stay dead until they’re all killed (and the re-spawn begins again). It seems like the intention is that it will only fill in one pass.
In terms of an alternative solution, I think iterating the children, finding all those that have yet to spawn, putting their indices in a list (provided the ordering of child objects is stable) and the populating them directly would work closer to the intended functionality. Alternatively, keeping track of the index of the last filled position and using it to make sure it’s only increasing until they’re all filled or something seems like it could also work.