Long spawn delays prevent SpawnUntilFull from completing

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.

1 Like

Good Observation @tobymurray

Another option perhaps would be to put a incremental variable (SpawnedEN) inside the foreach and spawn the enemies inside a if statement that prevents them to spawn if the SpawnedEN gets equal to the quantity of positions, and reset SpawnedEN everytime that all enemies are destroyed

That is a good point, @tobymurray.

It sounds like @Joao_Dalvi is suggesting what I would do, which is determine the # of positions needed to be filled only once, and then counting while spawning. It might still mean you would not have 100% consistent positions , but it seems simpler (I don’t know enough about Unity and children yet to know if tracking positions is better) and you at least would never spawn more than you should have.

1 Like

Hi,

here is my solution:

I declared and initialised two variables:

private int childIndex = 0;
private bool indexIncrementEN = false;

after this I modified the NextFreePosition and the AllMembersDead methods like this:

Transform NextFreePosition(){
	int positionIndex = 0;
	
	indexIncrementEN = !indexIncrementEN;
	foreach (Transform position in transform){
		if (position.childCount == 0){
			if (positionIndex >= childIndex){
				if (indexIncrementEN){
					childIndex++;
				}
				return position;
			}
		}
		positionIndex++;
	}
	return null;
}

bool AllMembersDead(){
	foreach (Transform position in transform){
		if (position.childCount > 0){
			return false;
		}
	}
	childIndex = 0;
	return true;
}

Now you can destroy enemies while they are spawning.

Regards,
Daniel

Thanks for this, however a problem occurs, if they don’t finish spawning when you kill one, it instantly spawns all of them at once, how do I fix this part?

Privacy & Terms