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.
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.
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.