[Solved]Enemy waves come with time ?

I'm trying to make enemy spawn for several time in every wave. what i got is a group of enemy come at once !!

Script link

What i want to do is on wave_1 " every 2 second, one enemy come up". wave_2 " every 5 second, 1 enemy come up"


  • Jonathan Gonzalez(jgonzalez) replied

    More than likely the Update method is the culprit. I added a Debug.Log message to each of your Spawn methods to see how often they get called, and it's quite a lot. This was taken at 20 seconds in:

    What's happening is that at 5 seconds, the InvokeRepeating gets called which waits 3 seconds. So in actuality the spawning of the enemies happens 8 seconds in. At the firs time it displayed 60 messages which means 60 enemies were spawned in, second time 120, and so on. It was also continuing even when the second wave "Spawn 2" was being called as well. 

    I would recommend using a Coroutine for this instead.  I create a script for this to help you out called Spawn Enemies.

    Essentially the way the script works is that it has most of the same things you had, including game time, spawn points and intervals of some sort. It uses coroutines, which give us more freedom with the timing and ensures that multiple enemies are not being spawned all at once. 

    I included a bool variable to determine when we can spawn as well. One coroutine determines the spawning of the enemies, while another coroutine determines the game time. While the "currentTime" is less than the total "gameTime" we'll continue incrementing currentTime to mimic a timer. Once these match, it'll set the bool "canSpawn" to false stopping the enemies from spawning. 

    When that happens it'll also reset the currentTime, increment the wave by 1, and restart the GameTimer coroutine which will restart the spawning process as well. 

    This is a similar approach to what was taught in this lesson Spawning Enemy Waves of the Developing a Tower Defense game course. 



  • AeleaS Omer(aeleas) replied

    Thank you jgonzalez, you cleared many things for me.

    I appreciate all what you do.