Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Can only kill one enemy.

Discussion in 'Scripting' started by Vexos, Jul 19, 2015.

  1. Vexos

    Vexos

    Joined:
    Jun 1, 2015
    Posts:
    19
    Hello, I've been stuck here for some hours now I've made a enemyhealth script and a "warriorattack" script which is working great in 1v1 but it stops working after the first kill.

    Here is the enemyhealth script.
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class EnemyHealth : MonoBehaviour
    5. {
    6.     public int EnemyStartingHealth = 100;
    7.     public int EnemyCurrentHealth;
    8.     bool isDead;
    9.  
    10.  
    11.         void Awake ()
    12.     {
    13.       //  enemyTarget = GetComponent <EnemyTarget> ();
    14.         EnemyCurrentHealth = EnemyStartingHealth;
    15.     }
    16.     void Update ()
    17.     {
    18. }
    19.     public void TakeDamage (int amount)
    20.     {
    21.         EnemyCurrentHealth -= amount;
    22.  
    23.  
    24.         if(EnemyCurrentHealth <= 0 && !isDead)
    25.         {
    26.             Destroy(GetComponent<TargetPlayer>());
    27.         }
    28.     }
    29.  
    30.        void Death ()
    31.    {
    32.      
    33.     }
    34. }
    35.  
    WarriorAttack script.
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class WarriorAttack : MonoBehaviour
    5. {
    6.  
    7.     public int attackDamage = 10;
    8.  
    9.     GameObject enemy;
    10.     EnemyHealth enemyHealth;
    11.  
    12.  
    13.     void Awake ()
    14.     {
    15.         enemy = GameObject.FindGameObjectWithTag ("Enemy");
    16.         enemyHealth = enemy.GetComponent <EnemyHealth> ();
    17.     }
    18.    
    19.  
    20.         void OnTriggerEnter (Collider collider)
    21.     {
    22.         if(collider.gameObject.tag == "Enemy");{
    23.           if(enemyHealth.EnemyCurrentHealth > 0)
    24.         {
    25.             enemyHealth.TakeDamage (attackDamage);
    26.         }
    27.     }
    28. }
    29. }
    30.  
    I have no clue what is going on, please anyone save me xD
     
  2. phoda

    phoda

    Joined:
    Nov 11, 2014
    Posts:
    384
    What exactly stops working? dealing damage? or something else
     
  3. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    That's because you're caching the enemyHealth in your WarriorAttack's Awake function. So it will only deal damage to that one enemy's health. You probably want to get the EnemyHealth component in your OnTriggerEnter function without the FindGameObjectWithTag call.
     
  4. HandOfPriam

    HandOfPriam

    Joined:
    Aug 19, 2010
    Posts:
    34
    Your current script only assigns an enemy once, when the script starts (during the Awake method). When the current enemy dies, the script never looks for a new enemy, so you will only ever be able to kill one. What you should do is check for a new enemy when the previous one dies.

    Try something like this. I don't know how you set up your scenes, but you can expand from here.
    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class WarriorAttack : MonoBehaviour
    5. {
    6.     public int attackDamage = 10;
    7.     GameObject enemy;
    8.     EnemyHealth enemyHealth;
    9.  
    10.     void Awake ()
    11.     {
    12.         enemy = GameObject.FindGameObjectWithTag ("Enemy");
    13.         enemyHealth = enemy.GetComponent <EnemyHealth> ();
    14.     }
    15.  
    16.     void OnTriggerEnter (Collider collider)
    17.     {
    18.         if(collider.gameObject.tag == "Enemy")
    19.         {
    20.  
    21.             if(enemyHealth.EnemyCurrentHealth > 0)
    22.             {
    23.                 enemyHealth.TakeDamage (attackDamage);
    24.             }
    25.  
    26.             else {
    27.                 enemy = GameObject.FindGameObjectWithTag ("Enemy");
    28.                 enemyHealth = enemy.GetComponent<EnemyHealth> ();
    29.             }
    30.         }
    31.     }
    32. }
    33.  
     
  5. Vexos

    Vexos

    Joined:
    Jun 1, 2015
    Posts:
    19

    Just noticed something, I cant kill the enemies in one order. It always starts with the orignial copy of it (ctrl + d).
    Maybe this is the problem?
     
  6. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    @Vexos you really need to read the above posts. And yes, FindGameObjectWithTag retrieves the first instance it's finds in the scene.
     
  7. Vexos

    Vexos

    Joined:
    Jun 1, 2015
    Posts:
    19
    @LeftyRighty Yeah just woke up so I'am not reading very well atm, any suggestions what I could use instead?
     
  8. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    line 22 doesn't need the ";", putting a semicolon after an if clause means the if doesn't govern anything...

    this will damage any enemy tagged gameobject with a trigger collider that the gameobject this script is attached to bumps into
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class WarriorAttack : MonoBehaviour
    6. {
    7.  
    8.     public int attackDamage = 10;
    9.  
    10.     void OnTriggerEnter (Collider other)
    11.     {
    12.         if(other.tag == "Enemy")
    13.         {
    14.             EnemyHealth enemyHealth = other.GetComponent<EnemyHealth>();
    15.             if(enemyHealth)
    16.             {
    17.                 enemyHealth.TakeDamage (attackDamage);
    18.             }
    19.         }
    20.     }
    21. }
    22.  
     
    Vexos likes this.
  9. Vexos

    Vexos

    Joined:
    Jun 1, 2015
    Posts:
    19
    My friend @LeftyRighty , you have saved this morning. Thank you very much! You do not know how grateful I'am, have a wonderfull morning/evening.