Check if enemies are alive?

How can i check if enemies are alive?
Thanks
Like what is the line of code to see if a enemy with tag “Enemy” is alive?

what do you do to determine if something is “alive” or “dead” in your game?

What does that even mean in the context of your game?

Not all games have “alive” things. Others represent “alive/dead” in different ways.

For example, maybe you have a ‘Health’ script, and in it a float that stores the amount of health something has. If it reaches 0, it’s considered “dead”. Well, in that case, you’d test that float if it’s less than 0.

Well I have a enemy spawner that spawns gameobjects all over the map.
I want to check if there are any gameobjects with he tag Enemy that are still active and if not re spawn.

GameObject[] enemies = GameObject.FindGameObjectsWithTag("Enemy");

This will only return active objects with the tag.

Here is what I have :slight_smile:
I want it do just wait until there are no enemies still alive then repeat the process :slight_smile:

using UnityEngine;
using System.Collections;

public class EnemySpawner : MonoBehaviour {
    public GameObject Enemy;
    public float timer = 0f;
    public float SpawnAreaGive = 1f;
    public bool enemiesAlive = false;

    // Use this for initialization
    void Start () {
   
    }
   
    // Update is called once per frame
    void Update () {
        timer += Time.deltaTime;
        if (timer >= 12) {
            GameObject[] enemySpawns = GameObject.FindGameObjectsWithTag("EnemySpawnPoint");
            foreach(GameObject spawnPoint in enemySpawns)
            {
                if(enemiesAlive == false){
                    GameObject enemy;
                   enemy = Instantiate(Enemy, new Vector3(spawnPoint.transform.position.x, spawnPoint.transform.position.y, spawnPoint.transform.position.z), spawnPoint.transform.rotation) as GameObject;
                   enemy.name = "Enemy";
                    enemiesAlive = true;
                    timer = 0;
                }
            }
        }
    }
}

Do I have to like say if there are no enemies alive then do that code or?
What lines and where? :slight_smile:

If you click the function name in the code it’ll bring you to the documentation:

It says:

If you’ve defined the idea of being “alive” as if the GameObject is ‘active’. Then this function will return all ‘active’, or “alive”, GameObjects.

So if it’s empty (length 0), no enemies exist.

By YOUR definition.

This is the critical part here… you’re defining the concept of “aliveness”. You control that definition. Use what function you have to your avail, and their definitions, to logically deduce that which you want.

Right now it spawns the first dose which do absolutely nothing because the actual values didn’t pass over and then they stay there…

I’m assuming you’re talking about from this other thread:

I know nothing of your code, your structure, or anything. Why won’t what hold what values?

Alright, the 2 things that won’t hold are the ‘Target’ from AdvancedAI and Camera from ‘Billboard’.
Whenever I have the enemy with everything attached and I try to make a prefab with them attached it removes those 2 objects.

Billboard:

using UnityEngine;
using System.Collections;

public class Billboard : MonoBehaviour {
    public Camera main_camera;

    // Update is called once per frame
    void Update () {
        transform.LookAt (transform.position + main_camera.transform.rotation * Vector3.back, main_camera.transform.rotation * Vector3.up);
    }
}

AdvancedAI:

using UnityEngine;
using System.Collections;

public class AdvancedAI : MonoBehaviour
{
    float distance;
   
    public float lookAtDistance = 25.0F;
    public float chaseRange = 2050.0F;
    public float attackRange =4.5F ;
    public float moveSpeed = 5.0F; 
    public float damping = 6.0F; 
    public float damage = 30.0F;
    public float attackRepeatTime = .8F;
    public Transform target;
    public CharacterController controller;
    private float verticalMomentum = 0f;
    private float gravity = -9.8f;
   
    private Vector3 moveDirection = Vector3.zero;
    private float attackTime;



    //TODO: add in a function to find controller and to locate and assign the player as the target
   
    void Start()
    {
        attackTime = Time.time;
    }
   
    void Update()
    {
        distance = Vector3.Distance(target.position, transform.position);
        verticalMomentum += gravity * Time.deltaTime;
       
        if(distance < lookAtDistance)
        {
            LookAt();
        }
       
        if (distance < attackRange)
        {
            AttackPlayer();
        }
       
        else if (distance < chaseRange)
        {
            ChasePlayer();

        }
    }
   
    void LookAt()
    {
        Quaternion rotation = Quaternion.LookRotation(target.position - transform.position);
        transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * damping);
    }
   
    void ChasePlayer()
    {
        moveDirection = transform.forward;
        moveDirection *= moveSpeed;
        moveDirection.y += verticalMomentum * Time.deltaTime;
        controller.Move(moveDirection * Time.deltaTime);
    }
   
    void AttackPlayer()
    {
        //TODO: Need Attack Animations
        if (Time.time > attackTime)
        {
            target.SendMessage("damagePlayer", damage, SendMessageOptions.DontRequireReceiver);
            attackTime = Time.time + attackRepeatTime;
        }
    }
   
    void ApplyDamage()
    {
        chaseRange += 15;
        moveSpeed += 1;
        lookAtDistance += 20;
    }
}

OK… ok… ummm:

I see you’ve been very busy today.

You’re probably just diving right into this right now, not a lot of programming experience under you belt, none of that stuff.

I suggest you go an start with some nice tutorials that walk you through these basic concepts. Instead of us holding your hand and taking a lot of time out of our own time to do so… use a tutorial that was already made to hold your hand. And then if you have any specific questions in regards to what you end up with at the end of the tutorial… come and ask us that.

I know @Kiwasi makes a bunch of videos on youtube, check em’ out:

1 Like

Yes… I plan to look at this.
Only thing is that I’m literally 90% done this game.
I have everything working besides my enemies.
If you could just answer this one last question I’m done haha :stuck_out_tongue:

Last question:
I have “enemies = 0”, what is the correct format to say that it returned a empty array?

using UnityEngine;
using System.Collections;

public class EnemySpawner : MonoBehaviour {
    public GameObject Enemy;
    public float timer = 0f;
    public float SpawnAreaGive = 1f;

    // Use this for initialization
    void Start () {
   
    }
   
    // Update is called once per frame
    void Update () {
        timer += Time.deltaTime;
        if (timer >= 12) {
            GameObject[] enemySpawns = GameObject.FindGameObjectsWithTag("EnemySpawnPoint");
            GameObject[] enemies = GameObject.FindGameObjectsWithTag("Enemy");
            foreach(GameObject spawnPoint in enemySpawns)
            {
                if(enemies = 0){
                    GameObject enemy;
                   enemy = Instantiate(Enemy, new Vector3(spawnPoint.transform.position.x, spawnPoint.transform.position.y, spawnPoint.transform.position.z), spawnPoint.transform.rotation) as GameObject;
                   enemy.name = "Enemy";
                    timer = 0;
                }
            }
        }
    }
}

lets go back to that documentation:

Lets read it… scrolling all the way to the end:

And then some code, showing, how to test an empty array.

It’s right there.

As the old abbreviation goes… RTFM.

Or of course, you could always… google it:
http://lmgtfy.com/?q=C%23+test+if+array+is+empty

1 Like
GameObject[] enemies = GameObject.FindGameObjectsWithTag"Enemy");
if (enemies.Length == 0)
{
    //Do code for all enemies being inactive.
}

If you want to do it per spawn point then it will require each spawn point to keep track of its own spawned enemies and then react when they are all inactive.

Keep in mind this code is not very performance friendly, and I would suspect from all the problems you have been having there are alot more spots of code that don’t work quite right or need optimizing. I thought about a project I was working when I read that you are 90% done your game, I thought wow I was thinking the same thing about my project. The problem is, it is nowhere near done. 90% of development is bug hunting and optimizing, so to say 90% done and have the problems you are having means that you are no where near done. Just a word from my experience.

2 Likes