Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Spawning waves of enemies after delay

Discussion in 'Scripting' started by ElectricMonkey, May 2, 2015.

  1. ElectricMonkey

    ElectricMonkey

    Joined:
    Mar 10, 2015
    Posts:
    12
    I'm trying to spawn waves of enemies. When each wave ends, there needs to be a break until the next wave starts.
    Code (csharp):
    1.  
    2.         public Transform[] spawnPoints;         // An array of the spawn points this enemy can spawn from.
    3.         public float waveTimer = 10f;
    4.         private float timeTillWave = 0.0f;
    5.         public int totalWaves = 5;
    6.         private int spawnCount = 0;
    7.  
    8.         private int numWaves = 0;
    9.         public static int numberOfEnemies = 0;
    10.         private int nrKilled;
    11.         public static bool waveFinished = false;
    12.        
    13.        
    14.  
    15.  
    16.         void Start ()
    17.         {
    18.             // Call the Spawn function after a delay of the spawnTime and then continue to call after the same amount of time.
    19.             waveFinished = false;
    20.             Spawn ();
    21.             //nextUsage = Time.time + delay;
    22.     }
    23.  
    24.  
    25.         void Spawn ()
    26.         {
    27.             // If the player has no health left...
    28.         if(playerHealth.currentHealth <= 0f || nrKilled == 2 || spawnCount == 3)
    29.             {
    30.                 // ... exit the function.
    31.             waveFinished = true;
    32.             //killItself ();
    33.                 return;
    34.             }
    35.  
    36.             // Find a random index between zero and one less than the number of spawn points.
    37.             int spawnPointIndex = Random.Range (0, spawnPoints.Length);
    38.  
    39.             // Create an instance of the enemy prefab at the randomly selected spawn point's position and rotation.
    40.             Instantiate (enemy, spawnPoints[spawnPointIndex].position, spawnPoints[spawnPointIndex].rotation);
    41.         spawnCount++;
    42.         }
    43.  
    44.         void Update()
    45.         {
    46.  
    47.         nrKilled = WaveManager.killedEnemies;
    48.  
    49.         if (waveFinished == true) {
    50.             nrKilled = 0;
    51.             spawnCount = 0;
    52.             StartCoroutine (Wait (10.0f));
    53.             Spawn ();
    54.             }
    55.         }
    56.        
    57.        
    58.  
    59.         void killItself()
    60.         {
    61.  
    62.         Destroy(gameObject);
    63.  
    64.  
    65.         }
    66.  
    67.         private IEnumerator Wait(float seconds)
    68.         {
    69.             Debug.Log("waiting");
    70.             yield return new WaitForSeconds(seconds);
    71.             Debug.Log("wait end");
    72.         }
    73.  
    74.  
    75.  
    76.  
    77.  
    78.    
    79.     }
    80.  
    They stop spawning, so the return in the Spawn function does get executed and one of the conditions does become true, but they don't start spawning again, which leads me to believe that the if condition in Update doesn't get executed for some reason?

    Any help would be appreciated.
     
  2. CodeMonke234

    CodeMonke234

    Joined:
    Oct 13, 2010
    Posts:
    181
  3. ElectricMonkey

    ElectricMonkey

    Joined:
    Mar 10, 2015
    Posts:
    12
  4. CodeMonke234

    CodeMonke234

    Joined:
    Oct 13, 2010
    Posts:
    181
    Sorry... Invoke could make your code cleaner and easier to read.

    The specific problem is that waveFinished is only set to true inside spawn()

    Spawn is only called from update when waveFinished is true.

    Start calls Spawn, but waveFinished is not set to true then.

    An alternative approach is to just call
    Invoke("SpawnEnemies", 3.0f);
    when the wave is finished.

    Regardless,
    you should move the conditional test for when the wave is finished into the update instead of in spawn...
    e,g
    1. if(playerHealth.currentHealth <= 0f || nrKilled == 2 || spawnCount == 3)
    2. {
    3. // ... exit the function.
    4. waveFinished = true;
    5. return;
    6. }
     
  5. ElectricMonkey

    ElectricMonkey

    Joined:
    Mar 10, 2015
    Posts:
    12
    Sorry, didn't want to come off hostile, really appreciate the help.
     
  6. pws-devs

    pws-devs

    Joined:
    Feb 2, 2015
    Posts:
    63
    I just want to point out that in your code, you actually are not waiting for 10 seconds before you spawn. In fact, you are calling Spawn without waiting. I suggest you read up on Coroutines again.