- Home /
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!!
Answer by Hellium · Jun 26, 2015 at 08:25 AM
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++;
}
}
@NEWBIE_1 : Take a look at the solution I proposed :
Attach the last script to an empty GameObject called GameController.
When an ennemy is instantiated, call the function EnemyHasAppeared() of this script When an enemy is killed, call the function EnemyHasDied() of the same script
Don't forget to add Application.LoadLevel("LevelName"); ins$$anonymous$$d of the comments of the EnemyHasDied() function.
i have made this so far, but how will the script know what objects are my enemies? I saw one person say if agemobject with tag "Enemy" is less than 0 Application.loadlevel("LevelName"); Is this something that is possible? Sorry im an artist just trying to make a simple game.
using UnityEngine; using System.Collections;
public class GameController : $$anonymous$$onoBehaviour { private int enemyLeft = 0;
public void EnemyHasDied()
{
enemyLeft--;
if (enemyLeft == 0)
{
Application.LoadLevel("Level_01");
}
}
public void EnemyHasAppeared()
{
enemyLeft++;
}
}
The GameController won't know it, the enemies themself will warn the GameController.
Do your enemies have a script on them ? If so :
Next to the attributes add :
public GameController GameController ;
Then, drag and drop the GameController object into the field inside the inspector
In their Start function, add :
GameController.EnemyHasAppeared()
In the function telling them to die (with the Destroy function is suppose), add :
GameController.EnemyHasDied()
Have you done the tutorials before trying to create your own game ?
ive watched the tutoial on making a survival game by unity projects. i am just getting really confused with all of this :(
The PROJECT: SURVIVAL SHOOTER ?
Have you watched the lessons in this page (below the projects) ?
https://unity3d.com/learn/tutorials/modules
I would advise you to watch the scripting videos if you plan to do logic stuff in your game. Don't try to script anything if you have never made programs before.
Answer by robbagus · Jun 26, 2015 at 08:28 AM
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?
I am really confused could someone write a straigh forward script that will allow me to accomplish the goal im trying to get?
Your answer

Follow this Question
Related Questions
How to Eliminate Monitor Artifacts When Loading Scene? 1 Answer
NullReferenceException and m_InstanceID == 0 on LoadLevel (C#) 1 Answer
How to make a script that changes level when you press enter at a door? 1 Answer
Log Callback after Scene change 1 Answer
Como fazer download de arquivos enquanto o jogo funciona 0 Answers