Rando enemy spawners not working

Hi, I am trying to make a random enemy spawner that spawns a random enemy at a random spawn point and only spawns enemies every X seconds and when there are no more than 10 enemies in the scene for some reason it not working. I tried to change several things and I still can’t find it to work.

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

public class EnemySpawner : MonoBehaviour
{
    public Transform[] spawnPoints;
    public GameObject[] enemyPrefabs;
    
    public int spawnCount;
    public int spawnLimit;

    public bool spawn;
    public float startSpawnTime;
    public float spawnTime;   

    // Start is called before the first frame update
    void Start()
    {
        spawnTime = startSpawnTime;
    }

    // Update is called once per frame
    void Update()
    {
        if(!spawn && spawnLimit < spawnCount)
        {
            spawnTime = 0f;
            startSpawnTime -= Time.deltaTime;
            Spawner();
            spawn = true;
        }
       
    }

    void Spawner()
    {
        if (spawnTime < 0 )
        {
            int randEnemy = Random.Range(0, enemyPrefabs.Length);
            int randEnemySpawners = Random.Range(0, spawnPoints.Length);

            spawnCount++;

            Instantiate(enemyPrefabs[randEnemy], spawnPoints[randEnemySpawners].position, transform.rotation);
        }
        else { spawnTime -= startSpawnTime; }
    }
}

imagen_2022-06-18_150710557
imagen_2022-06-18_150723582

Hi,

Welcome to our community! It’s great to see that you are challenging yourself. :slight_smile:

How do you know that the code stops working after 10 enemies have been spawned? Have you already tried to add Debug.Logs to your code to see what is going on during runtime?

How do you keep track of the active enemies in the scene (“no more than 10 enemies in the scene”)? The simplest solution would be to parent the instantiated enemies to a root game object and check the childCount of said root game object.

Is there a reason why spawnTime -= startSpawnTime; is used twice in the code?

Hi, thanks for the response

I tried to use this: spawnCount++; to start counting the enemies that spawn and that once it reaches 10 they stop spawning for the If in updated, the problem I have is that it does not spawn any enemy directly, and the Debug.Log, does not bring me any message so the code for some reason is not being activated directly.

In which part of the code I used 2 times spawnTime -= startSpawnTime; I am not able to see where .

Well I think I just find the solution, I remove spawnTime = startSpawnTime; from the Start()
and now is used in the Update(), SpawnTime = 0f; and spawn = false these are the last 2 functions to be activated and in the Spawner() function I added && spawnLimit < spawnCount on the If()
Now is look like this:

void Start()
    {
       
    }

    // Update is called once per frame
    void Update()
    {
        if(!spawn)
        {
            spawnTime = startSpawnTime;
            startSpawnTime -= Time.deltaTime;
            Spawner();
            spawn = true;
            spawnTime = 0f;
            spawn = false;
        }
       
    }

    void Spawner()
    {
        if (spawnTime >= 0 && spawnCount <= spawnLimit)
        {
            int randEnemy = Random.Range(0, enemyPrefabs.Length);
            int randEnemySpawners = Random.Range(0, spawnPoints.Length);

            spawnCount++;

            Instantiate(enemyPrefabs[randEnemy], spawnPoints[randEnemySpawners].position, transform.rotation);
        }
        else { spawnTime -= startSpawnTime; }
    }
}

I tested it and so far is working fine, Thanks for the help Nina

Fantastic. I’m glad you solved the problem yourself. :slight_smile:

In regards to my question about the duplicated lines, I made a mistake. For some reason, I read Time.deltaTime as startSpawnTime.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.