Home > Enterprise >  Enemy Spawner While Loop Not Spawning Correct Number Of Enemies
Enemy Spawner While Loop Not Spawning Correct Number Of Enemies

Time:08-24

I am trying to automatically spawn a desired amount of enemies within an area, which is currently a test area. The idea is that if the current amount of enemies are less than the amount of desired enemies, then the keepSpawning boolean is set to true which activates the while loop.

Below is my current script, but the issue is that on start, it doesn't instantiate the correct amount of enemies. Additionally, while the game is running, it doesn't instantiate the correct amount of enemies. I have a minus 1 for the enemiesCurrentCount variable when an enemy is killed, and the while loop does replace the killed enemy which is great, but still does not instantiate to the desire amount of enemies.

This is the result I get from setting the enemies desired value;

enemiesDesired Resulting amount of enemies
1 0
2 1
3 1
4 2
5 2
6 3
7 3
8 3
9 3
10 4
11 4
12 5
13 5
14 6

Code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SpawnEnemies : MonoBehaviour
{
        //Enemies array
        public GameObject[] Enemies;
        public int enemiesInArray;

        //Spawn enemy within these positions on the map
        private int xPos;
        private int zPos;
        private int yPos;

        private int randomEnemy;

        public static int enemiesCurrentCount; //Amount of enemies in game currently
        public int enemiesDesired; //Amount of enemies desired in game at the same time

        public bool keepSpawning;

        //Wait timers
        public float startWaitSeconds;
        public float spawnWaitSeconds;


    void Start()
    {
        enemiesCurrentCount = 0;
        keepSpawning = true;
    }

    void Update()
    {
        //Check if new enemies should be spawned
        if (enemiesCurrentCount <= enemiesDesired)
        {
            keepSpawning = true;
            StartCoroutine(RandomSpawnEnemies());
        }
        else
        {
            keepSpawning = false;
        }
    }

    //Spawn Enemies
    IEnumerator RandomSpawnEnemies()
    {
        yield return new WaitForSeconds(startWaitSeconds);

        while (keepSpawning)
        {
            randomEnemy = Random.Range(0, enemiesInArray);
            xPos = Random.Range(-10, 8);
            zPos = Random.Range(17, 30);
            Instantiate(Enemies[randomEnemy], new Vector3(xPos, yPos, zPos), Quaternion.identity);
            enemiesCurrentCount  = 1;
            yield return new WaitForSeconds(spawnWaitSeconds);
        }
    }

}

As a test and from comments received, I have amended the code to run the spawner only on start, just to see if it would produce the desired amount of enemies, but still it doesn't. I tried to make the code re-producable also for better assistance however you'll still require an 'enemy' to add and also change the co-ordinates so they spawn in the area you have.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SpawnEnemies : MonoBehaviour
{
        //Enemies array
        public GameObject[] Enemies;
        public int enemiesInArray;

        //Spawn enemy within these positions on the map
        private int xPos;
        private int zPos;
        private int yPos;

        private int randomEnemy;

        public int enemiesCurrentCount = 0; //Amount of enemies in game currently
        public int enemiesDesired = 5; //Amount of enemies desired in game at the same time

        //Wait timers
        public float spawnWaitSeconds;


    void Start()
    {
        StartCoroutine(SpawnRandomEnemies());
    }

    void Update()
    {

    }

    //Spawn Enemies
    IEnumerator SpawnRandomEnemies()
    {
        while (enemiesCurrentCount < enemiesDesired)
        {
            randomEnemy = Random.Range(0, enemiesInArray);
            xPos = Random.Range(-10, 8);
            zPos = Random.Range(17, 30);
            Instantiate(Enemies[randomEnemy], new Vector3(xPos, 1, zPos), Quaternion.identity);
            enemiesCurrentCount  = 1;
            yield return new WaitForSeconds(0.1f);
        }
    }
}

The new above code sets the enemy count at 0, the desired count at 5, but still only spawns 2 enemies.

CodePudding user response:

It happens because when it runs the RandomSpawnEnemies and check the condition after startWaitSeconds seconds the Update method have already set keepSpawning to false. You can verify it removing the yield return new WaitForSeconds(startWaitSeconds);.

CodePudding user response:

I manage to fix it so the code now works. Thanks to everyone for your help and guidance, it helped me achieve a working result.

I resolved the under spawning issue by changing enemiesCurrentCount from an integer to a double, and then I changed enemiesCurrentCount = 1; to enemiesCurrentCount = 0.5; and now it works perfectly.

After various additional tests to determine why this change works but the code that is seemingly fine does not, I concluded that the issue is with the enemy game object I was using which was downloaded from Unity asset store.

I changed my code back again and attempted to spawn enemies using a capsule game object as a test, and it works perfectly.

Final code if anyone would like it, to spawn their own enemies;

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SpawnEnemies : MonoBehaviour
{
        //Enemies array
        public GameObject[] Enemies;
        public int enemiesInArray;

        //Spawn enemy within these positions on the map
        private int xPos;
        private int zPos;
        private int yPos;

        private int randomEnemy;

        public int enemiesCurrentCount; //Amount of enemies in game currently
        public int enemiesDesired; //Amount of enemies desired in game at the same time

        //Wait timers
        public float spawnWaitSeconds;


    void Start()
    {
        StartCoroutine(SpawnRandomEnemies());
    }

    void Update()
    {

    }

    //Spawn Enemies
    IEnumerator SpawnRandomEnemies()
    {
        while (enemiesCurrentCount < enemiesDesired)
        {
            randomEnemy = Random.Range(0, enemiesInArray);
            xPos = Random.Range(-10, 8);
            zPos = Random.Range(17, 30);
            Instantiate(Enemies[randomEnemy], new Vector3(xPos, 1, zPos), Quaternion.identity);
            enemiesCurrentCount  = 1;
            yield return new WaitForSeconds(0f);
        }
    }

}
  • Related