Enemy not spawn if is more then like 3 ??

Hello i have a enemySpawn script here

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

public class EnemySpawner : MonoBehaviour {
   
    public GameObject enemy;               
    public float spawnTime = 10f;           
    public Transform[] spawnPoints;         

    void Start()
    {       
        InvokeRepeating("Spawn", spawnTime, spawnTime);
    }


    void Spawn()
    {               
        int spawnPointIndex = Random.Range(0, spawnPoints.Length);
       
        Instantiate(enemy, spawnPoints[spawnPointIndex].position, spawnPoints[spawnPointIndex].rotation);
    }
}

but now i want it only to spawn enemys if there is less then three…
ply help :slight_smile:

If you want to have only 3 spawned enemy, you can track exists enemies adding them on spawn to collection, remove on death, and put flag before spawning which’ll check if there is 3 enemies in collection or not.

okey :slight_smile: how do i do it :slight_smile: like

if enemy >= 3 stopspawn ? or something ?

void Spawn()
{         
  // if there are more than 3 enemies, return - so, prevent execution of spawn code
    if(enemy > 3)
    {
        return; 
    }
   
    int spawnPointIndex = Random.Range(0, spawnPoints.Length);
      
    Instantiate(enemy, spawnPoints[spawnPointIndex].position, spawnPoints[spawnPointIndex].rotation);
}

it dosent work :frowning:

Dude have you done the tutorials yet? Because this is about as basic as it gets:

    void Spawn()
    {
        int spawnPointIndex = Random.Range(0, spawnPoints.Length);
        if (enemy < 3)
        {
            Instantiate(enemy, spawnPoints[spawnPointIndex].position, spawnPoints[spawnPointIndex].rotation);
            enemy++;
        }
        else
            Debug.Log("Won't spawn more than 3 enemies");
    }

but it dosent work x)

Please be more descriptive than “it doesn’t work”. Also I didn’t notice you already used enemy as a GameObject.

I’ve never used InvokeRepeating; I always use Coroutines.

Try this:

public class EnemySpawner : MonoBehaviour
{

    public GameObject enemy;
    public float spawnTime = 10f;
    public Transform[] spawnPoints;
    public int enemyCount;

    void Start()
    {
        StartCoroutine(SpawnEnemyTimer(spawnTime));
    }


    void Spawn()
    {
        int spawnPointIndex = UnityEngine.Random.Range(0, spawnPoints.Length);
        if (enemyCount < 3)
        {
            Instantiate(enemy, spawnPoints[spawnPointIndex].position, spawnPoints[spawnPointIndex].rotation);
            enemyCount++;
        }
    }

    IEnumerator SpawnEnemyTimer(float time)
    {
        yield return new WaitForSeconds(time);
        Spawn();
        StartCoroutine(SpawnEnemyTimer(spawnTime));
    }
}

@DroidifyDevs : your coroutine would run only once.

OP: On one of the enemy scripts, keep a variable for the Enemy Spawner. When you instantiate your game object, assign the reference of the spawner to the newly created enemy. When it dies, call a method (on Enemy Spawner) that will subtract 1 from the enemy count.

So, once the enemy count is at 3, the coroutine should continue to yield the wait time. Once the count drops below that, it should spawn them (again).

Also, as mentioned, please be more descriptive in what’s wrong when something is not working as you want it to. We’re not mind readers :wink:

1 Like

This is wrong approach, enemies should not know about the EnemySpawner class.

Then you would suggest what to keep track of the number of alive enemies? It would be a good follow up for your post, if you included something you thought was a good alternative. :slight_smile:

Yeah, my post sounds a bit weird without offering some alternative, it is just too late here.

Well, in context with the other code something like

if (GetComponentsInChildren<EnemyController>().Length > 3)
    return;
else
    // Instantiate another enemy

will be more then sufficient I think.

Oh okay, I thought you were going to recommend an event (which I thought might be complicated for the OP).

I wasn’t sure that the enemies spawned were children. If they are, then sure that’s good, too. :slight_smile:

Well at the end of the day, it’s up to OP to determine how he wants to do this. He hasn’t even replied to my code (I’ve edited it after @methos5k pointed out the coroutine issue I overlooked, thanks) yet. Personally, what I’d do is when an enemy dies, a script on the enemy can simply access the enemyCount in EnemySpawner class. For OP:

public EnemySpawner ES;

    void OnEnemyDeath()
    {
        ES.enemyCount--;
    }

I think that’s the easiest way to do this.