- Home /
Im trying to make a Java script that detects how many tags of Enemy are still alive. How exactly do i do that?
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");
} }
Im not in front of my cpu but you can have a variabl enemy count and each time an enemy die do enemycount --; if enemycount == 0 load new level. You can also use event to achieve this
If you use GameObject.FindObjectsWithTag(), youll need to call this function each time an enemy die and it will take a lot of "power" for nothing
@Crazydadz - It is my understanding (and my experience) that, unlike GameObject.Find(), FindObjectsWithTag() is efficient enough that it can be called every frame. This is especially true if the number of tagged objects is small. But in the case above, it doesn't need to be called every frame. Probably four times a second would work just fine.
Your solution is unarguably more efficient, but it comes with its own set of issues and complexity. You have to figure out how to communicate the creation and kills of enemies to wherever the code above lives. So we are talking about a Singleton, or a static variable (with its own set of issues), or C# events or taking the "death" away from the individual enemy and handling it somewhere central. All involved more complexity than I though was helpfulto the OP based on the code posted.
Answer by Ice Koobs · May 08, 2013 at 02:34 AM
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
if (GameObject.FindGameObjectsWithTag("Enemy").Length == 0)
Application.Loadlevel("win");
Thanks! But the LevelControl thing says this compiler error: An instance of type 'LevelControl' is required to access non static member 'enemiesLeft'.
$$anonymous$$y mistake. I've corrected the script. The variable should have 'static' not 'public'.
Everything works, but when I load the first level, the function update goes to fast and loads 'win' already. But when i go on the second level and die, and start back at the first, the function update does nothing and I can play. But when all the Enemies are killed, it doesn't load 'win'. But everything else works, so thank you in advance.
It's set off before you even killed the enemies or as soon as the level starts?
Answer by Loius · May 09, 2013 at 06:57 PM
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");
Your answer
Follow this Question
Related Questions
Loading new level after killing enemies 3 Answers
How do you create a "collect" objection that corresponds with the enemy behavior in C#? 1 Answer
How to stop enemy shooting through wall 1 Answer
Model Problem pls help. 0 Answers
force traslation 1 Answer