How can I spawn another game object once one has been destroyed?

I am currently using this script to spawn a limit of two game objects.

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

public class newSpawn : MonoBehaviour {

	public Transform[] spawnPoints;      
	public GameObject enemy;
	public int enemyCount = 0;
	public int enemyLimit = 2;

void Start () {

		startInvoke ();

	void startInvoke()
		InvokeRepeating ("spawn", 0f, 5f);

	void spawn()

		int spawnPointIndex = Random.Range (0, spawnPoints.Length); 

		if(enemyCount >= enemyLimit) return;

		Instantiate (enemy, spawnPoints[spawnPointIndex].position, spawnPoints[spawnPointIndex].rotation);




How would I then tell the script to spawn another game object once one is destroyed, keeping the total at 2?

Any help much apprecated.

You can use the Update function to keep track of when an object is destroyed, and when that happens you can call the spawn function again.

I don’t see you destroying the object in this script, but wherever that is happening, update the enemyCount variable over there (maybe declare it static if object is destroyed in a different script), and then use Update function to spawn a new one. It will look somewhat like this


And then in current script you introduce a new function

void Update() {
if(enemyCount < enemyLimit)

I think if you use the Update function, you might not need to use InvokeRepeating(), but that completely depends on what you are trying to achieve.

You create 2 object, and hide them, then you just have to unhide each of them by their index and time they exist appropiately.