Loading Level when last enemy dies.

I am new to programming as i am an artist. I am trying to create a C# that will load a new scene/level when the last enemy dies. I dont know where to start… My enemies are prefabs and get spawned in a certain amount after a certain time. i have tags for each player “Enemy1” “Enemy2” “Enemy3”. Please and thank you!!

You could use something like this :

using UnityEngine;
using System.Collections;

public class Enemy : MonoBehaviour
{
    // Number of enemies alive
	private static int aliveCounter = 0 ;

	void Start()
	{
		aliveCounter++ ;
	}
	
	void OnKilled()
	{
		aliveCounter-- ;
		
		if( aliveCounter == 0 )
			Application.LoadLevel("NextLevel") ;
	}

}

Where the OnKilled function is obviously called when your enemy is killed.

The aliveCounter attribute is a static integer, common to all instances of the class Enemy. All your enemies will share the value of this attribute.


Note to @robbagus 's code : This code must be held by a GameController while the solution I proposed must be attached to an enemy directly.

Though, I think that the “death check” isn’t well thought. The enemy who has just died should call a function of the GameController. The same way, another function will be necessary to warn the GameController a new Enemy has appeared :

//C#

using UnityEngine;
using System.Collections;

public class GameController : MonoBehaviour
{

    private int enemyLeft = 0 ;

    public void EnemyHasDied()
    {
        enemyLeft--;
     
        if (enemyLeft == 0)
        {
            // do your thing here, loading scene etc
        }
    }

    public void EnemyHasAppeared()
    {
        enemyLeft++;
    }
}

The most straight forward solution is to have an integer counter checking the amount of enemies left.

int enemyLeft = 5; // sample number

if (enemyDie) // do your enemy death check here
{
    enemyLeft--;
}

if (enemyLeft == 0)
{
   // do your thing here, loading scene etc
}

Although, I’m a bit confused why would you have different tags for your enemies since they are all the same (prefab). Are you giving each enemy different behavior later on?