Forum begins after the advertisement:


[Part 12] Enemy Spawner messed up

Home Forums Video Game Tutorial Series Creating a Rogue-like Shoot-em Up in Unity [Part 12] Enemy Spawner messed up

Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
    Posts
  • #18638
    Bruno Azalim
    Level 4
    Participant
    Helpful?
    Up
    0
    ::

    I am following this series for some time and everything’s working, but when i reached this part my enemy spawner started to spawn just the first wave then stop. it was working fine before that but since then it has stopped. funny thing is that we havent changed the enemy spawner script since before it got broke. any ideas of what could be happening and how to fix it? . this video is about weapon evolution and ive revamped the system with the part 15 already but this issue was not fixed. i dont even know which scripts to paste here

    will leave the enemyspawner code here anyways

    using NUnit.Framework;
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    public class EnemySpawner : MonoBehaviour
    {
        [System.Serializable]
        public class Wave
        {
            public string waveName;
            public List<EnemyGroup> enemyGroups;  //list of groups of enemies to spawn in this wave
            public int waveQuota;  //total number of enemies spawned in this wave
            public float spawnInterval;  //the interval at which to spawn enemies
            public int spawnCount;  //number of enemies already spawned in this wave
        }
    
        [System.Serializable]
        public class EnemyGroup
        {
            public string enemyName;
            public int enemyCount;  //number of enemies to spawn in this wave
            public int spawnCount;  //number of enemies of this type already spawned in this wave
            public GameObject enemyPrefab;
        }
    
        public List<Wave> waves;  //a list of all the waves in the game
        public int currentWaveCount;  //the index of the current wave (starts from 0)
    
        [Header("Spawner Attributes")]
        float spawnTimer; //how long between each spawn
        public int enemiesAlive;
        public int maxEnemiesAllowed; //max number of enemies allowed on the map at once
        public bool maxEnemiesReached = false;
        public float waveInterval; //interval between each wave
        bool isWaveActive = false;
    
        [Header("Spawn Positions")]
        public List<Transform> relativeSpawnPoints; // a list to store all the relative spawn positions
    
        Transform player;
    
    
        void Start()
        {
            player = FindFirstObjectByType<PlayerStats>().transform;
            CalculateWaveQuota();
        }
    
    
        void Update()
        {
            //check if wave has ended and next wave should start
            if(currentWaveCount < waves.Count && waves[currentWaveCount].spawnCount == 0 && !isWaveActive)
            {
                StartCoroutine(BeginNextWave());
            }
    
            spawnTimer += Time.deltaTime;
    
            //check if its time to spawn the next enemy
            if(spawnTimer >= waves[currentWaveCount].spawnInterval)
            {
                spawnTimer = 0;
                SpawnEnemies();
            }
        }
    
        IEnumerator BeginNextWave()
        {
            isWaveActive = true;
    
            //waveInterval seconds before starting next wave
            yield return new WaitForSeconds(waveInterval);
    
    
            //if there are more waves to start after current wave, move on to the next wave
            if(currentWaveCount < waves.Count - 1)
            {
                isWaveActive = false;
                currentWaveCount++;
                CalculateWaveQuota();
            }
    
        }
    
        void CalculateWaveQuota()
        {
            int currentWaveQuota = 0;
            foreach (var enemyGroup in waves[currentWaveCount].enemyGroups)
            {
                currentWaveQuota += enemyGroup.enemyCount;
            }
    
            waves[currentWaveCount].waveQuota = currentWaveQuota;
        }
    
        /// <summary>
        /// this method will stop spawning enemies if the amount has reached its maximum
        /// </summary>
        void SpawnEnemies()
        {
            //check if the minimum number of enemies in the wave have been spawned
            if (waves[currentWaveCount].spawnCount < waves[currentWaveCount].waveQuota && !maxEnemiesReached)
            {
                //spawn each type of enemy until the quota is filled
                foreach (var enemyGroup in waves[currentWaveCount].enemyGroups)
                {
                    //check if the minimum number of enemies of this type have been spawned
                    if(enemyGroup.spawnCount < enemyGroup.enemyCount)
                    {
                        //defines the location of spawning the enemies out of the screen
                        Instantiate(enemyGroup.enemyPrefab, player.position + relativeSpawnPoints[Random.Range(0, relativeSpawnPoints.Count)].position, Quaternion.identity);
    
                        enemyGroup.spawnCount++;
                        waves[currentWaveCount].spawnCount++;
                        enemiesAlive++;
    
                        //limit the number of enemies that can be spawned at once
                        if (enemiesAlive >= maxEnemiesAllowed)
                        {
                            maxEnemiesReached = true;
                            return;
                        }
                    }
                }
            }
        }
    
        //call this function when an enemy is killed
        public void OnEnemyKilled()
        {
            //decrease the number of enemies alive
            enemiesAlive--;
    
            //reset maxEnemiesReached flag to false if number of enemies alive has dropped below max amount
            if (enemiesAlive < maxEnemiesAllowed)
            {
                maxEnemiesReached = false;
            }
        }
    }
    
    #18641
    Terence
    Level 31
    Keymaster
    Helpful?
    Up
    0
    ::

    There shouldn’t have been any changes to the EnemySpawner script in Part 15. I suspect the issue might’ve been some of your Inspector settings.

    @Alp.Apustaja can you highlight some of the key settings Bruno has to look out for in the Inspector if his EnemySpawner is only spawning 1 wave?

    #18643
    Bruno Azalim
    Level 4
    Participant
    Helpful?
    Up
    1
    ::

    thank you for the quick response terence! this problem was actually happening since part 12

    has upvoted this post.
    #18644
    Terence
    Level 31
    Keymaster
    Helpful?
    Up
    0
    ::

    No problem bruno :) let’s see what the AI says. It is trained with all the content from the series so it is much faster at spotting issues / errors than I am.

    #18650
    Alp Apustaja
    Level 10
    AI Assistant
    (BETA)
    Helpful?
    Up
    1
    ::

    Based on the reference information and the provided script, here are key Inspector settings to check:

    1. Waves List
    • Ensure multiple waves are actually configured
    • Check that each wave has:
      • Populated enemyGroups
      • Correct waveQuota
      • Proper spawnInterval
    1. Wave Spawning Parameters
    • maxEnemiesAllowed: Verify it’s set high enough to allow multiple wave spawns
    • waveInterval: Make sure it’s not set too high, preventing wave progression
    • relativeSpawnPoints: Confirm spawn points are assigned
    1. Enemy Group Configuration
    • Each enemyGroup should have:
      • A valid enemyPrefab
      • Correct enemyCount
      • Matching enemyCount and spawnCount logic

    These settings are critical to ensure the EnemySpawner can progress through multiple waves successfully.

    has upvoted this post.
    #18653
    Alp Apustaja
    Level 10
    AI Assistant
    (BETA)
    Helpful?
    Up
    0
    ::

    Could you clarify a few details about your wave configuration in the Unity Inspector?

    1. How many waves have you set up?
    2. What are the specific enemy counts for each wave?
    3. What value do you have set for maxEnemiesAllowed?
    4. Have you confirmed that the OnEnemyKilled() method is being called when enemies die?

    These details will help diagnose why your enemy spawner might be stopping after the first wave.

    #18666
    Bruno Azalim
    Level 4
    Participant
    Helpful?
    Up
    1
    ::

    ive checked all parameters that you said and the error was in the maxEnemiesAllowed count, it was too low. now its working as intended, thank you!

    has upvoted this post.
    #18667
    Alp Apustaja
    Level 10
    AI Assistant
    (BETA)
    Helpful?
    Up
    1
    ::

    The reference information highlights that the maxEnemiesAllowed parameter directly impacts enemy spawning. When this value is set too low, it can prematurely halt the enemy spawning process by triggering the maxEnemiesReached flag. Increasing this value allows more enemies to spawn simultaneously, resolving the wave progression issue.

    has upvoted this post.
Viewing 8 posts - 1 through 8 (of 8 total)
  • You must be logged in to reply to this topic.

Go to Login Page →


Advertisement below: