- Home /
Question by
jasonunityzuum · Jan 31, 2014 at 05:39 AM ·
levelsenemies
New Level (Scene) after Enemies are gone
Hey my name is Jason and i am new to Unity I am making a small Arena Game were you have to kill a lot of enemies in different levels. If you kill all enemies in "Level1" you should get to "Level2". I am searching for a script that can automaticcaly change the Level if all of "Enemy" "Enemy2" and "Enemy3" are gone. Can somebody please help me?
Comment
if(condition where all Enemies are gone)
Application.Loadlevel("nextLevel");
Do you have any control structures (List, Array, etc) that is already tracking EnemyX?
Best Answer
Answer by Nick4 · Jan 31, 2014 at 09:39 AM
C# :
bool nextLevel;
void Update()
{
nextLevel = true;
foreach(Collider co in Physics.OverlapSphere(Vector3 position, float radius))
{
if(co.gameObject.tag == "Enemy")
{
nextLevel = false;
}
}
if(nextLevel)
{
Application.LoadLevel(int levelIndex);
}
}
Physics.OverlapSphere will cast a sphere collider and return all colliders in the area. Make sure radius is big enough to return all possible enemies' collider and enemies have the tag "Enemy". Position value better be somewhere in the middle.
JS version :
var nextLevel : boolean;
function Update()
{
nextLevel = true;
foreach(Collider co in Physics.OverlapSphere(position : Vector3, radius : float))
{
if(co.gameObject.tag == "Enemy")
{
nextLevel = false;
}
}
if(nextLevel)
{
Application.LoadLevel(levelIndex : int);
}
}