Spawn enemy if no other enemies are from the spawnposition

I have two types of enemies and four enemy spawn positions(each types has two spawn position). I want that a new enemy spawn when the player and one of the 4 enemies is dead and at least one of the four spawn position is empty.

I want to controll the EnemySpawnPosition if it’s empty or not.

The script is attachted to one of the enemy spawn position and it only work for the one(I know that the enemycount is different, that was just a prototype script):

using UnityEngine;
using System.Collections;

public class EnemyCount : MonoBehaviour {
	
	public int enemyCount = 2;
	public GameObject spawnObject; //public GameObject[] spawnObject;
	public Transform spawnPosition; //public Transform[] spawnPosition;
	
	private int i = 0;
	
	public HealthController healthController;
	
	// Use this for initialization
	void Start () {
		spawnObject.GetComponent<EnemyHealthController>().enemycount = GameObject.Find("EnemySpawn");
		spawnObject.GetComponent<EnemyHealthController>().expSystem = GameObject.Find("Player");
		spawnObject.GetComponent<SendDamageCollider> ().expSystem = GameObject.Find ("Player");
		spawnObject.GetComponent<SendDamageCollider> ().enemycount = GameObject.Find ("EnemySpawn");
		spawnObject.transform.GetChild (0).GetComponent<stomp> ().controller = GameObject.Find ("Player");
	}
	
	// Update is called once per frame
	void Update () {
		if(enemyCount <= 1 && healthController.respawn)
		{
			Spawn();
		}
	}
	
	void Spawn()
	{
		if(enemyCount <= 1)
		{
			enemyCount = 2;
			Instantiate(spawnObject,spawnPosition.position,Quaternion.identity);   			
		} 		
	}	
}

I never tried something like that but the most easy way that i can think of is:
Make a bool (canSpawn) that turns false when an enemie is in the trigger collider of the spawnpoint. When the enemie dies OR when the enemie leaves the trigger zone, the bool turns true. When the bool is true, a new enemie will be spawned.

bool canSpawn = false;

void OnTriggerExit()
{
 canSpawn = true;
}

void OnTriggerEnter()
{
 canSpawn = false;
}

void Die()
{
 canSpawn = true;
}

void SpawnEnemie()
{
 canSpawn = false;
}

So canSpawn turns false when: Enemie is entering the zone OR enemie is instantiated
canSpawn turns true when: Enemie leaves the zone OR enemie dies

Check out Physics.OverlapSphere

It returns all the game objects within a specified sphere. You can specify a layer mask too if need be.

You can either:

Just call OverlapSphere, specifying the spawn point position and radius, then check the returned list of objects for enemies

Or, put all your enemies in their own layer, then just call TestSphere, specifying a layer mask, it’s similar, except it just returns true if there’s anything in the sphere or false otherwise.

I’d write you some code but am on my iPad. Let me know if you need it - I’ll check tomorrow when I have a keyboard handy!

EDIT: Some code…

//this function takes a 3d position, a radius and a layer
//name, and returns true if there are any objects inside the
//layer within a distance of spawn_point_radius from spawn_point_pos
bool TestObjectsInLayer(Vector3 spawn_point_pos, float spawn_point_radius, string layer_name)
{
    //first we get the layer id (will be from 0 to 31 depending on how you set it up in the editor)
    int layer_id = LayerMask.NameToLayer(layer_name);

    //next we construct a 'bit mask' - a 32 bit value, where 0 bits indicate 'ignore layer'
    //and 1 bits indicate 'look in this layer'. We set just the bit for our layer. So if
    //layer is  4, we set the 4th bit to 1. This is done by shifting the value '1' left by
    //layer id bits.
    int layer_mask = 1 << layer_id;

    //finally, we use the Physics.CheckSphere function to search for objects near the spawn point
    return Physics.CheckSphere(spawn_point_pos, spawn_point_radius, layer_mask);
}

That’s a general function for look for objects in a given layer. There is a corresponding version for the 2d physics system (you didn’t say if you were using 2d or 3d colliders), which will look almost identical, but they changed the api slightly just to be annoying!..

    //this function takes a 2d position, a radius and a layer
    //name, and returns true if there are any objects inside the
    //layer within a distance of spawn_point_radius from spawn_point_pos
    bool TestObjectsInLayer(Vector2 spawn_point_pos, float spawn_point_radius, string layer_name)
    {
        //first we get the layer id (will be from 0 to 31 depending on how you set it up in the editor)
        int layer_id = LayerMask.NameToLayer(layer_name);
    
        //next we construct a 'bit mask' - a 32 bit value, where 0 bits indicate 'ignore layer'
        //and 1 bits indicate 'look in this layer'. We set just the bit for our layer. So if
        //layer is  4, we set the 4th bit to 1. This is done by shifting the value '1' left by
        //layer id bits.
        int layer_mask = 1 << layer_id;
    
        //finally, we use the Physics2D.OverlapCircle function to search for objects near the spawn point
        //note: just to be confusing, the 2d api onll provides the 'Overlap' version, which actually returns
        //the first collider it finds in the circle, or null otherwise. so we call it and return true
        //if any collider was found
        return Physics2D.OverlapCircle (spawn_point_pos, spawn_point_radius, layer_mask) != null;
    }

With those functions all you need to do is make sure your objects have colliders, and then place them in their own layer (maybe called ‘Enemies’). Then call the function, passing in the spawn point location, size of the area you want to check and the name of the layer you put your objects in. The functions will return true if any objects are already there.

Hope that helps. Sorry if there’s any compile errors - haven’t actually tried the code, but I have written it lots of times over the years :slight_smile: