Issue with Multiple Enemies Interacting with Player Health

So I have an enemy script and when it comes to dealing damage to the player it only works when there is one instance of the enemy. I don’t know why it happens because if there are 2 enemies/prefabs that have the same script, only one of them is able to get the health manager script from the player game object and set the take2dmg to true. Then if the working enemy (enemy that actually sets take2dmg in the health manager script to true) is deleted, the previously not working enemy suddenly works correctly. Here are the scripts relating to this problem and it specifically has to do with the parts relating to the take2dmg bool:

Health script for player:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class HealthManager : MonoBehaviour
{
    public int Health = 10;
    Animator Tick1;
    Animator Tick2;
    Animator Tick3;
    Animator Tick4;
    Animator Tick5;
    Animator Tick6;
    Animator Tick7;
    Animator Tick8;
    Animator Tick9;
    Animator Tick10;
    Animator HitEffect;
    Animator CamHolder;
    public int DmgAmount = 0;

    public bool take2dmg = false;
    // Start is called before the first frame update
    void Start()
    {
        Tick1 = GameObject.Find("Healthtic1").GetComponent<Animator>();
        Tick2 = GameObject.Find("Healthtic2").GetComponent<Animator>();
        Tick3 = GameObject.Find("Healthtic3").GetComponent<Animator>();
        Tick4 = GameObject.Find("Healthtic4").GetComponent<Animator>();
        Tick5 = GameObject.Find("Healthtic5").GetComponent<Animator>();
        Tick6 = GameObject.Find("Healthtic6").GetComponent<Animator>();
        Tick7 = GameObject.Find("Healthtic7").GetComponent<Animator>();
        Tick8 = GameObject.Find("Healthtic8").GetComponent<Animator>();
        Tick9 = GameObject.Find("Healthtic9").GetComponent<Animator>();
        Tick10 = GameObject.Find("Healthtic10").GetComponent<Animator>();
        HitEffect = GameObject.Find("HitEffect").GetComponent<Animator>();
        CamHolder = GameObject.Find("CamHolder").GetComponent<Animator>();

    }

    // Update is called once per frame
    void Update()
    {

        if (take2dmg)
        {
            DmgAmount = 2;
            StartCoroutine(takeDMG());
            StartCoroutine(CamDmgEffect());
        }
  
        
        if (Health == 10)
        {
            Tick1.SetBool("Dis", false);
            Tick2.SetBool("Dis", false);
            Tick3.SetBool("Dis", false);
            Tick4.SetBool("Dis", false);
            Tick5.SetBool("Dis", false);
            Tick6.SetBool("Dis", false);
            Tick7.SetBool("Dis", false);
            Tick8.SetBool("Dis", false);
            Tick9.SetBool("Dis", false);
            Tick10.SetBool("Dis", false);
        }
        else if (Health == 9)
        {
            Tick1.SetBool("Dis", false);
            Tick2.SetBool("Dis", false);
            Tick3.SetBool("Dis", false);
            Tick4.SetBool("Dis", false);
            Tick5.SetBool("Dis", false);
            Tick6.SetBool("Dis", false);
            Tick7.SetBool("Dis", false);
            Tick8.SetBool("Dis", false);
            Tick9.SetBool("Dis", false);
            Tick10.SetBool("Dis", true);
        }
        else if (Health == 8)
        {
            Tick1.SetBool("Dis", false);
            Tick2.SetBool("Dis", false);
            Tick3.SetBool("Dis", false);
            Tick4.SetBool("Dis", false);
            Tick5.SetBool("Dis", false);
            Tick6.SetBool("Dis", false);
            Tick7.SetBool("Dis", false);
            Tick8.SetBool("Dis", false);
            Tick9.SetBool("Dis", true);
            Tick10.SetBool("Dis", true);
        }
        else if (Health == 7)
        {
            Tick1.SetBool("Dis", false);
            Tick2.SetBool("Dis", false);
            Tick3.SetBool("Dis", false);
            Tick4.SetBool("Dis", false);
            Tick5.SetBool("Dis", false);
            Tick6.SetBool("Dis", false);
            Tick7.SetBool("Dis", false);
            Tick8.SetBool("Dis", true);
            Tick9.SetBool("Dis", true);
            Tick10.SetBool("Dis", true);
        }
        else if (Health == 6)
        {
            Tick1.SetBool("Dis", false);
            Tick2.SetBool("Dis", false);
            Tick3.SetBool("Dis", false);
            Tick4.SetBool("Dis", false);
            Tick5.SetBool("Dis", false);
            Tick6.SetBool("Dis", false);
            Tick7.SetBool("Dis", true);
            Tick8.SetBool("Dis", true);
            Tick9.SetBool("Dis", true);
            Tick10.SetBool("Dis", true);
        }
        else if (Health == 5)
        {
            Tick1.SetBool("Dis", false);
            Tick2.SetBool("Dis", false);
            Tick3.SetBool("Dis", false);
            Tick4.SetBool("Dis", false);
            Tick5.SetBool("Dis", false);
            Tick6.SetBool("Dis", true);
            Tick7.SetBool("Dis", true);
            Tick8.SetBool("Dis", true);
            Tick9.SetBool("Dis", true);
            Tick10.SetBool("Dis", true);
        }
        else if (Health == 4)
        {
            Tick1.SetBool("Dis", false);
            Tick2.SetBool("Dis", false);
            Tick3.SetBool("Dis", false);
            Tick4.SetBool("Dis", false);
            Tick5.SetBool("Dis", true);
            Tick6.SetBool("Dis", true);
            Tick7.SetBool("Dis", true);
            Tick8.SetBool("Dis", true);
            Tick9.SetBool("Dis", true);
            Tick10.SetBool("Dis", true);
        }
        else if (Health == 3)
        {
            Tick1.SetBool("Dis", false);
            Tick2.SetBool("Dis", false);
            Tick3.SetBool("Dis", false);
            Tick4.SetBool("Dis", true);
            Tick5.SetBool("Dis", true);
            Tick6.SetBool("Dis", true);
            Tick7.SetBool("Dis", true);
            Tick8.SetBool("Dis", true);
            Tick9.SetBool("Dis", true);
            Tick10.SetBool("Dis", true);
        }
        else if (Health == 2)
        {
            Tick1.SetBool("Dis", false);
            Tick2.SetBool("Dis", false);
            Tick3.SetBool("Dis", true);
            Tick4.SetBool("Dis", true);
            Tick5.SetBool("Dis", true);
            Tick6.SetBool("Dis", true);
            Tick7.SetBool("Dis", true);
            Tick8.SetBool("Dis", true);
            Tick9.SetBool("Dis", true);
            Tick10.SetBool("Dis", true);
        }
        else if (Health == 1)
        {
            Tick1.SetBool("Dis", false);
            Tick2.SetBool("Dis", true);
            Tick3.SetBool("Dis", true);
            Tick4.SetBool("Dis", true);
            Tick5.SetBool("Dis", true);
            Tick6.SetBool("Dis", true);
            Tick7.SetBool("Dis", true);
            Tick8.SetBool("Dis", true);
            Tick9.SetBool("Dis", true);
            Tick10.SetBool("Dis", true);
        }
        else if (Health == 0)
        {
            Tick1.SetBool("Dis", true);
            Tick2.SetBool("Dis", true);
            Tick3.SetBool("Dis", true);
            Tick4.SetBool("Dis", true);
            Tick5.SetBool("Dis", true);
            Tick6.SetBool("Dis", true);
            Tick7.SetBool("Dis", true);
            Tick8.SetBool("Dis", true);
            Tick9.SetBool("Dis", true);
            Tick10.SetBool("Dis", true);
        }

    }

    IEnumerator takeDMG()
    {
        HitEffect.SetBool("HitEffect", false);
        HitEffect.SetBool("HitEffect", true);
        yield return new WaitForSeconds(0.42f);
        HitEffect.SetBool("HitEffect", false);

    }

    IEnumerator CamDmgEffect()
    {
        CamHolder.SetBool("HitShk", true);
        yield return new WaitForSeconds(0.235f);
        CamHolder.SetBool("HitShk", false);

    }
}

Script for Enemy:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class FryAITest : MonoBehaviour
{
    private float jumpforce = 12;
    public float jumpforceHoriz = 12;
    Animator anim;
    Rigidbody2D rb;
    public bool moveRight = false;
    Renderer renda;
    public int health = 15;
    public GameObject b1;
    public GameObject b2;
    public GameObject b3;
    GameObject Player;
    public bool InRange = false;
    public bool pound = false;
    public bool HitPlayer = false;
    
    HealthManager PlrHealth;
    // Start is called before the first frame update
    void Start()
    {
        anim = gameObject.GetComponent<Animator>();
        rb = gameObject.GetComponent<Rigidbody2D>();
        Transform childTransform = transform.Find("Group 68 Merged");

        renda = childTransform.GetComponent<Renderer>();

        PlrHealth = GameObject.Find("FinalHolder").GetComponent<HealthManager>();

        Player = GameObject.Find("FinalHolder");
    }

    // Update is called once per frame
    void Update()
    {
        
        if (transform.position.x < -9)
        {
            moveRight = true;
            
        }
        if (transform.position.x > 9)
        {
            moveRight = false;

        }


        if (moveRight)
        {
            transform.localScale = new Vector3(-1, transform.localScale.y, transform.localScale.z);
        }
        else
        {
            transform.localScale = new Vector3(1, transform.localScale.y, transform.localScale.z);
        }

        if (health <= 0)
        {
            b1.SetActive(true);
            b2.SetActive(true);
            b3.SetActive(true);
            anim.SetBool("Dead", true);
        }
        else
        {
            b1.SetActive(false);
            b2.SetActive(false);
            b3.SetActive(false);
        }

        if (transform.position.x < Player.transform.position.x + 0.7f && transform.position.x > Player.transform.position.x - 1f)
        {
            InRange = true;
        }
        else
        {
            InRange = false;
        }

        if (InRange && pound)
        {
            PlrHealth.take2dmg = true;
        }
        else
        {
            PlrHealth.take2dmg = false;
        }
        
        

        
    }
    void Jump()
    {
        if (moveRight)
        {
            rb.AddForce(Vector2.up * jumpforce, ForceMode2D.Impulse);
            rb.AddForce(Vector2.right* jumpforceHoriz, ForceMode2D.Impulse);
        }
        else
        {
            rb.AddForce(Vector2.up * jumpforce, ForceMode2D.Impulse);
            rb.AddForce(Vector2.left * jumpforceHoriz, ForceMode2D.Impulse);
        }
            
    }
    IEnumerator hit()
    {
        health = health - 1;
        float duration = 0.3f; 

        Color startColor = new Color(1f, 0.31f, 0.31f); 
        Color endColor = new Color(1f, 1f, 1f); 

        float elapsedTime = 0f;

        while (elapsedTime < duration)
        {
            renda.material.color = Color.Lerp(startColor, endColor, elapsedTime / duration);
            elapsedTime += Time.deltaTime;
            yield return null; 
        }

        renda.material.color = endColor; 

    }

    private void OnCollisionEnter2D(Collision2D collision)
    {
        if (collision.gameObject.tag == "Bullet")
        {
            StartCoroutine(hit());
        }
        
    }

    void destroy()
    {

        Destroy(gameObject);
    }

    void poundtrue()
    {

        pound = true;
    }
    void poundfalse()
    {

        pound = false;
    }

    

}

1 Answer

1

I could be misunderstanding, but it seems like both of your enemy scripts in your stated example could be working. There is a single bool in the health script, so once that is set to true in a single frame it will always just deal 2 damage.

Perhaps you could see if having the enemy script directly edit the DmgAmount would fix your problem?

In your health script update function (replacing the if (take2dmg) statement:

 if (DmgAmount > 0)
        {
            take2dmg = true;
            StartCoroutine(takeDMG()); //Set DmgAmount = 0; and take2dmg = false; at the end of this coroutine
            StartCoroutine(CamDmgEffect());
        }

And in your enemy script:

if (InRange && pound && ! PlrHealth.take2dmg)
        {
            PlrHealth.DmgAmount += 2;
        }//Remove else

I suggest this assuming that you are wanting the 2 enemies to deal 4 damage instead of a 2 damage.