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

Resolved duplicates of enemies wont die?

Discussion in 'Scripting' started by trans_rights, Apr 17, 2023.

  1. trans_rights

    trans_rights

    Joined:
    Apr 13, 2023
    Posts:
    2
    Very new to this, so forgive me if im missing something obvious. I am able to kill one of my enemies, but the other ones dont die. i cant see where the problem would be with my scripts, can someone help?

    heres the code for my enemy health
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Health : MonoBehaviour
    6. {
    7.     public int currentHealth = 20;
    8.     public static bool isEnemyDead = false;
    9.  
    10.     public void TakeDamage(int damageAmount)
    11.     {
    12.         currentHealth -= damageAmount;
    13.  
    14.         if (currentHealth <= 0 && isEnemyDead == false)
    15.         {
    16.             Debug.Log("Dead:" + currentHealth);
    17.             gameObject.GetComponent<Animator>().Play("Dying");
    18.             gameObject.GetComponent<enemyai>().speed = 0;
    19.             GetComponent<Animator>().enabled = false;
    20.             Destroy(gameObject, 1);
    21.             isEnemyDead = true;
    22.         }
    23.     }
    24. }
    25.  
    and heres the code for my bullet, im using collision between the bullet and enemy to deal damage.
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class BulletCollision : MonoBehaviour
    6. {
    7.     private void OnCollisionEnter(Collision collision)
    8.     {
    9.         if (collision.transform.tag == "Enemy")
    10.         {
    11.             collision.gameObject.GetComponent<Health>().TakeDamage(5);
    12.             Debug.Log("owch");
    13.         }
    14.     }
    15. }
    16.  
    again, sorry if im wasting anyone's time because this is an easy solution, ive tried everything (that i could find or think of)
     
  2. Olipool

    Olipool

    Joined:
    Feb 8, 2015
    Posts:
    316
    The problem is line 8 because you use a static variable. That is shared between EVERY Health script or to be more precise: it is not a variable that belongs to a health object but to the class.
    The effect is the same, when the first enemy dies, you set it to true. After that, when the currentHealth of another enemy is below zero, the check in line 14 will fail because isenemyDead is already true.
    Remove the static keyword and by that each enemy gets their own bool variable and that should work.
     
  3. trans_rights

    trans_rights

    Joined:
    Apr 13, 2023
    Posts:
    2
    thank you!! i really appreciate your help :)