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

Bug Why is it that when one enemy dies, both of them stop moving?

Discussion in 'Scripting' started by Wolfgang385, Dec 20, 2022.

  1. Wolfgang385

    Wolfgang385

    Joined:
    Nov 11, 2022
    Posts:
    6
    Here is the code I use for patrol enemies and enemy health. When one dies, the other becomes 1 tap and stops moving.
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class EnemyHealth : MonoBehaviour
    6. {
    7.  
    8.     public Animator anim;
    9.    
    10.     public int maxHealth = 100;
    11.     public static int currentHealth;
    12.    
    13.     // Start is called before the first frame update
    14.     void Start()
    15.     {
    16.         currentHealth = maxHealth;
    17.     }
    18.  
    19.     public void TakeDamage(int damage)
    20.     {
    21.         currentHealth -= damage;
    22.  
    23.         if (currentHealth > 0)
    24.         {
    25.             anim.SetTrigger("Hurt");
    26.             // Play hurt animation
    27.         }
    28.  
    29.         if (currentHealth <= 0)
    30.         {
    31.             Die();
    32.         }
    33.     }
    34.     void Die()
    35.     {
    36.         Debug.Log("Enemy died!");
    37.  
    38.         GetComponent<Collider2D>().enabled = false;
    39.  
    40.         anim.SetBool("isDead", true);
    41.     }
    42. }
    43.  
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Patrol : MonoBehaviour
    6. {
    7.     public float speed;
    8.    
    9.     private bool movingRight = false;
    10.  
    11.     public Transform groundDetection;
    12.  
    13.     void Update()
    14.     {
    15.         transform.Translate(Vector2.left * speed * Time.deltaTime);
    16.  
    17.         RaycastHit2D groundInfo = Physics2D.Raycast(groundDetection.position, Vector2.down, 2f);
    18.  
    19.         if (groundInfo.collider == false)
    20.         {
    21.             if (movingRight)
    22.             {
    23.                 transform.eulerAngles = new Vector3(0, -180, 0);
    24.                 movingRight = false;
    25.             }
    26.             else
    27.             {
    28.                 transform.eulerAngles = new Vector3(0, 0, 0);
    29.                 movingRight = true;
    30.             }
    31.         }
    32.         if (EnemyHealth.currentHealth <= 0)
    33.         {
    34.             speed = 0;
    35.         }
    36.     }
    37. }
    38.  
     
  2. HauntingWharf31

    HauntingWharf31

    Joined:
    Aug 16, 2021
    Posts:
    1
    I would think your two enemies are sharing one health variable. So when the one dies the other one thinks it can no longer move. Could be wrong though.
     
  3. nmcconnell

    nmcconnell

    Joined:
    May 4, 2014
    Posts:
    2
    @HauntingWharf31 is correct - all your enemies share the same health, because it is declared static
    If you remove the "static" there it should probably work better.
     
    Chubzdoomer likes this.
  4. Wolfgang385

    Wolfgang385

    Joined:
    Nov 11, 2022
    Posts:
    6
    tysm! This helped a lot