Home > database >  stop instantiating prefab if hit limit but continue if prefabs get destroyed
stop instantiating prefab if hit limit but continue if prefabs get destroyed

Time:06-19

i have a wave system set up and it instantiates a specific amount of enemy prefabs, however say there are supposed to be 100 enemies in total that have to be instantiated on said wave, thats a lot for the player to handle so i want to set a limit (doing it like in COD: Zombies) for how many prefabs are allowed to be active at a time.

    IEnumerator SpawnWave (Wave _wave)
    {
        for (int i = 0; i < _wave.count; i  )
        {
            if (curZombiesSpawned < maxZombiesSpawned)
            {
                SpawnEnemy();
                Debug.Log("spawnenemy");
            }
            yield return new WaitForSeconds( 1f/_wave.rate );
        }

        state = SpawnState.WAITING;
        yield break;
    }

    void SpawnEnemy()
    {
        Transform _sp = spawnPoints[Random.Range(0, spawnPoints.Length)];
        PhotonNetwork.InstantiateRoomObject (enemyObject.name, _sp.position, _sp.rotation);
        curZombiesSpawned  ;
    }

before i instantiate the enemy i want to check if the currently spawned enemies is less than the max amount of enemies spawned at a time.

but lets say on x wave there are supposed to be 50 enemies, but the limit is 24, i only want 24 to spawn, and when i kill x amount of enemies, i want to spawn more enemies till it reaches the limit again. doing that until all 50 are dead, then the next wave starts again

current problem: if the enemy amount on x wave is greater than the limit, it spawns x amount of enemies (spawn the amount of what the limit is set to) but when i kill some (thus destroy the object) they dont keep spawning again.

CodePudding user response:

You can try something like this:

    IEnumerator SpawnWave (Wave _wave)
    {
        int spawnedEnemiesCount = 0;
        while(spawnedEnemiesCount < _wave.count)
        {
            while (curZombiesSpawned < maxZombiesSpawned && spawnedEnemiesCount < _wave.count)
            {
                SpawnEnemy();
                spawnedEnemiesCount  ;
                Debug.Log("spawnenemy");
            }
            yield return new WaitForSeconds( 1f/_wave.rate );
        }
    }
  • Related