How to destroy a only enemy?

Hello, guys. How to destroy a only enemy that have same scripts that others enemies? Because if i hit one enemy, all enemies are affected.(Sorry for my english).

Post your script.

1 Like

public float mana;

void Update () {
if (Vector3.Distance (Player.transform.position, this.gameObject.transform.position) < 3.0f && delayAtaq <= 0.7f) {
transform.LookAt (Player.transform.position);

print (“Atacou”);
mana -= danamage;
playerStats.atacando = true;
danoPlayer = Random.Range (0, 3);
if (danoPlayer <= 1) {
playerStats.dano = 0.001f;

}
if (danoPlayer >= 2) {
playerStats.dano = 0.005f;

}
print (playerStats.dano);
playerStats.vida -= playerStats.dano;
} else {
playerStats.atacando = false;

}
if(delayAtaq >= 2){
delayAtaq = 0;
}
}

First you need to find all enemies and place them in an array. Then you need to check for the component by looping through that array. It would look something like this:

GameObject[] enemies = GameObject.FindGameObjectsWithTag("Enemies"); //get all of the enemies (You will need to tag all enemies with "Enemies")

for (int i = 0; i < enemies.Length; i++) //loop through the enemies
{
     if (enemies[i].GetComponent<ScriptName>()) //check if this enemy has this component
     {
          //if so, do something here
     }
}
2 Likes

It’s work. But now the all enemy’s HUD are affected. I did the same as you spoke for HUD enemies, but don’t work! Why? :confused:

using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class HudEnemy : MonoBehaviour {
    public Image imagem;
    private Player player;
    public GameObject[] enemies;


    // Use this for initialization
    void Start () {
        imagem = gameObject.GetComponent<Image> ();
        enemies = GameObject.FindGameObjectsWithTag ("Inimigo")();
        player = GameObject.FindGameObjectWithTag ("Player").GetComponent<Player> ();



    }

    // Update is called once per frame

    void Update () {
        if (player.atacando) {
            for (int i = 0; i < enemies.Length; i++) //loop through the enemies
            {
                if (enemies[i].GetComponent<Inimigo>()) //check if this enemy has this component
                {
                    if(Vector3.Distance(enemies[i].transform.position, player.transform.position) <3){
                    imagem.fillAmount = enemies[i].GetComponent<Inimigo>().mana;
                    }
          
                }
            }
        }
    }
}