Issue with Spawning enemies (creates Clone on original Enemy!)

Not sure why this problem doesn’t occur for Ben at the end of this video:
When you run the code at the end of the previous lecture, it prints “Spawned” right at the beginning -> reason: the CoRoutine creates a Spawn event immediately after starting the game!
So when you actually Instantiate an Enemy in this lecture, you create a Clone/Spawn of the enemy at the Exact same location of the original enemy! When your turrets destroy the first enemy, then it destroys the original enemy, but not the clone in the same space!
So, because Ben didn’t test the code before fixing the Turrets (!), he didn’t notice that it would kill the first enemy, but there’s another one in its place, and the turrets would have stopped firing, even though there’s an enemy onscreen at that location.
Note: **Perhaps you solve this in the next lesson, but I’ve just spent 3 hours figuring out this and another related bug, where I had thousands of spawning enemies exploding everywhere, crashing my computer. Couldn’t take a screenshot :frowning: **

SOLUTION
Add a delay to your CoRoutine: In the EnemySpawner code, in the EnemySpawner function, add the line:
yield return new WaitForSeconds(secondsBetweenSpawns);
right before the while (true) loop.
That will delay the CoRoutine for secondsBetweenSpawns, but only do it once (the first time).

Thanks for sharing your solution. :slight_smile:

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms