Spawner with max enemies

Hi, I’ll try keep this as detailed as I can but short.

I’m trying to make a spawner that has max amount of enemies it can spawn say 10, I got it to the point were it will spawn 1 enemy and if he dies it spawns another but when it comes to spawning more than 1 thats were it gets annoying.

var enemy1 : Transform;

function Update () {
var enemyTarget = GameObject.FindWithTag("Enemy");

if( enemyTarget == null )

function SpawnEnemy()
   Instantiate(enemy1, transform.position, transform.rotation);

This is the code I’ve got, I looked all over the internet and I tried Invoke and that didn’t work, I tried While(true) but that just failed and crashed Unity o_O. So I’m at a lost with this :(.

Right now you have an enemy instance stored in a variable, and when it is null you spawn another one. So, this code will only ever spawn one enemy at a time. There’s really any number of ways you can solve this problem because it’s just a programming problem, but here’s one way you can do it for the sake of example:

//This is a behaviour to stick in your enemy prefab
public class SpawnedObject : Monobehaviour {

    public ObjectSpawner homeSpawn;

    void OnDestroy() {

//This is the spawner
public class ObjectSpawner : MonoBehaviour {

    //object you want to spawn
    SpawnedObject objectPrefab;

    //maximum allowed number of objects - set in the editor
    public int maxObjects = 1;

    //number of objects currently spawned
    public int spawnCount = 0;
    void Update() {
        if(maxObjects>spawnCount) {

    void SpawnEnemy() {
        SpawnedObject obj = 
        obj.homeSpawn = this;

Take a look, I’m in a rush right now and can’t properly explain the code but try it and see what happens when you delete an object.

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

public class SpawnTest : MonoBehaviour {
	public List<GameObject> spawnList;
	// Use this for initialization
	void Start () 
		spawnList = new List<GameObject>();
		InvokeRepeating("CheckAlive", 1, 1);
		//Populate for the first time
		for (int i = 0; i < 10; i++)
			spawnList.Add(new GameObject(Time.timeSinceLevelLoad.ToString()));
	void CheckAlive()
		for (int i = 0; i < spawnList.Count; i++)
  •  	{*

_ spawnList = GameObject.CreatePrimitive(PrimitiveType.Cube);_
_ spawnList*.name = Time.timeSinceLevelLoad.ToString();