I have a tag named enemy. The script is supposed to detect how many enemy tags are alive and if there are none, the game loads the next scene.
var Enemy = tag {
if (Enemy = 0) {
Application.Loadlevel(“win”);
}
}
I have a tag named enemy. The script is supposed to detect how many enemy tags are alive and if there are none, the game loads the next scene.
var Enemy = tag {
if (Enemy = 0) {
Application.Loadlevel(“win”);
}
}
You could use the following:
var enemiesLeft:int;
function Update()
{
for(var badGuys : GameObject in GameObject.FindGameObjectsWithTag("Enemy"))
{//Find anything with the tag Enemy
enemiesLeft++;
//Every time you find an enemy add it to a the total number of enemies
}
Debug.Log(enemiesLeft);
//Use Debug.Log to keep a track of numbers while testing and debuging.
//Delete once you know the code is sound
if (enemiesLeft==0)
{
Application.Loadlevel("win");
}
}
But Crazydadz is correct. This will take up a bit of juice.
You’re better doing something along the lines of the following
Have a script called “LevelControl”(this is important for later):
static var enemiesLeft[:int;
function Start()
{//This will happen once at the beginning of each level just to find total number
for(var badGuys : GameObject in GameObject.FindGameObjectsWithTag("Enemy"))
{
enemiesLeft++;
}
}
function Update()
{
if (enemiesLeft==0)
{
Application.Loadlevel("win");
}
}
Then have a script attached to the enemy that when they’re destroyed the following line is actioned.
LevelControl.enemiesLeft--;
//When dead take one away from the enemiesLeft variable in the script LevelControl
Hope that solves your problem.
Koobs
What I do with pretty much all my instantiable classes now is this:
using System.Collections.Generic;
public class Mine : MonoBehaviour {
public static List<Mine> instances = new List<Mine>();
void OnEnable() {
instances.Add(this);
}
void OnDisable() {
instances.Remove(this);
}
}
...
Debug.Log("There are currently " + Mine.instances.Count + " mines");