Search Unity

How to destroy a only enemy?

Discussion in 'Community Learning & Teaching' started by Rodrigofbm, Jul 4, 2015.

  1. Rodrigofbm

    Rodrigofbm

    Joined:
    Dec 9, 2014
    Posts:
    3
    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).
     
  2. Gametyme

    Gametyme

    Joined:
    May 7, 2014
    Posts:
    618
    Post your script.
     
    Rodrigofbm likes this.
  3. Rodrigofbm

    Rodrigofbm

    Joined:
    Dec 9, 2014
    Posts:
    3
    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;
    }
    }
     
  4. renaissanceCoder1

    renaissanceCoder1

    Joined:
    Apr 8, 2015
    Posts:
    127
    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:

    Code (CSharp):
    1. GameObject[] enemies = GameObject.FindGameObjectsWithTag("Enemies"); //get all of the enemies (You will need to tag all enemies with "Enemies")
    2.  
    3. for (int i = 0; i < enemies.Length; i++) //loop through the enemies
    4. {
    5.      if (enemies[i].GetComponent<ScriptName>()) //check if this enemy has this component
    6.      {
    7.           //if so, do something here
    8.      }
    9. }
     
    theANMATOR2b and Rodrigofbm like this.
  5. Rodrigofbm

    Rodrigofbm

    Joined:
    Dec 9, 2014
    Posts:
    3
    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? :/


    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4. using UnityEngine.UI;
    5.  
    6. public class HudEnemy : MonoBehaviour {
    7.     public Image imagem;
    8.     private Player player;
    9.     public GameObject[] enemies;
    10.  
    11.  
    12.     // Use this for initialization
    13.     void Start () {
    14.         imagem = gameObject.GetComponent<Image> ();
    15.         enemies = GameObject.FindGameObjectsWithTag ("Inimigo")();
    16.         player = GameObject.FindGameObjectWithTag ("Player").GetComponent<Player> ();
    17.  
    18.  
    19.  
    20.     }
    21.  
    22.     // Update is called once per frame
    23.  
    24.     void Update () {
    25.         if (player.atacando) {
    26.             for (int i = 0; i < enemies.Length; i++) //loop through the enemies
    27.             {
    28.                 if (enemies[i].GetComponent<Inimigo>()) //check if this enemy has this component
    29.                 {
    30.                     if(Vector3.Distance(enemies[i].transform.position, player.transform.position) <3){
    31.                     imagem.fillAmount = enemies[i].GetComponent<Inimigo>().mana;
    32.                     }
    33.          
    34.                 }
    35.             }
    36.         }
    37.     }
    38. }
    39.