- Home /
How to keep count of enemies left
How I can check how many gameobjects with tag: "enemy" there is left in the scene, and after that it gets to zero then it will change level to another scene. I'm learning c# so I would prefer help on that language.
Did you get a script that loads a new level when your enemies are dead?
Change void OnGui() to look like...
void OnGUI()
{
if(killedAllEnemies)
{
Application.LoadLevel("YourLevelNameHere");
}
else
{
GUI.Label(new Rect (0,0,200,20),"Enemies Remaining : " + enemiesLeft);
}
}
Answer by bubzy · Jul 20, 2013 at 12:00 AM
using UnityEngine;
using System.Collections;
public class gameobjects : MonoBehaviour {
// Use this for initialization
int enemiesLeft = 0;
bool killedAllEnemies = false;
void Start () {
enemiesLeft = 10; // or whatever;
}
// Update is called once per frame
void Update () {
GameObject[] enemies = GameObject.FindGameObjectsWithTag("enemy");
enemiesLeft = enemies.Length;
if(Input.GetKeyDown(KeyCode.A))
{
enemiesLeft --;
}
if(enemiesLeft == 0)
{
endGame();
}
}
void endGame()
{
killedAllEnemies = true;
}
void OnGUI()
{
if(killedAllEnemies)
{
GUI.Label(new Rect (0,0,200,20),"all gone");
}
else
{
GUI.Label(new Rect (0,0,200,20),"Enemies Remaining : " + enemiesLeft);
}
}
}
something like this maybe.
Using an Update loop with FindGameObjectsWithTag is a processor intensive solution, and will suck up a few ms of frame time. A better way would be to have the enemies that die report that fact to whatever is keeping track of the tally. When the tally reaches 0, you could fire an event so that anything that cares about all of your enemies being dead can react. I typed this code in the comment field, so it's no guaranteed to compile, but give you an idea of what I'm talking about.
public delegate void AllEnemies$$anonymous$$illed();
public static event OnAllEnemies$$anonymous$$illed;
public GameObject[] enemyPrefabs;
private List<Enemy> enemies;
void SpawnEnemies(int count)
{
for (int i = 0; i < count; i++)
enemies.Add(GameObject.Instantiate(enemyPrefab[Random.Range(0, enemyPrefabs.Length));
}
public void Enemy$$anonymous$$illed(Enemy enemy)
{
if (enemies.Contains(enemy))
{
enemies.Remove(enemy);
if (enemies.Count == 0 && OnAllEnemies$$anonymous$$illed != null)
OnAllEnemies$$anonymous$$illed();
}
}
Then, on your Enemy script:
void OnDestroy()
{
GameController.Enemy$$anonymous$$illed(this);
}
Answer by fesi · Aug 23, 2017 at 08:29 AM
Array1=GameObject.findGameObjectsWithTag("Enemy"); void Start(){ Array1=GameObject.findGameObjectsWithTag("Enemy"); } Void Update(){ Array1=GameObject.findGameObjectsWithTag("Enemy"); textBox.text=""+array1.length+ "/" + array1.length; } @Reefer ,take another array of game object like totalEnemies and save the enemy tag in it, and then assign it to your text field like this TargetTextBox.text=""+totalEnemies.length; and also assign same enemy tag to enemy game object array in start function. this will give you something like this Enemies: total/remaining.
Your answer
Follow this Question
Related Questions
Enter next level 1 Answer
Load a scene/level when enemy is close... 1 Answer
good resource about conserving memory when multiple levels share resources? 0 Answers
How to restart a "prefab'd" level? ;) 0 Answers
Load Scene by audio clip 2 Answers