Duplicate object and shooting problem (SOLVED)

I’m learning to make a 2D platformer. I have a gun rotating around the mouse pointer and shooting at the direction of the mouse pointer when the left mouse button is clicked. The flying enemy has a Box Collider 2D on it. When i use the gun to kill the original flying enemy, it worked pretty well, the flying enemy died. But after i created a duplicate of my original flying enemy, something weird happened. If i shoot at the duplicate one, it would die like normal. But if i shoot the original one first, the duplicate one will be taken damage and will die first then if i keep shooting at it, the original one will be taken damage and die like normal. Can someone point out my problem here? Thank you for taking your time to read my question.
Here is the script i attached to the gun.

void FireRocket()
    {
        Vector2 mouPos = new Vector2(Camera.main.ScreenToWorldPoint(Input.mousePosition).x, Camera.main.ScreenToWorldPoint(Input.mousePosition).y);

        Vector2 gunTipPosition = new Vector2(gunTip.position.x, gunTip.position.y);

        RaycastHit2D hit = Physics2D.Raycast(gunTipPosition, mouPos - gunTipPosition);
if (hit.collider != null)
        { 
            if(hit.collider.tag=="Flying Enemies")
            {
                  FindObjectOfType<MakeDead>().RemoveHealth(damage);
            }
        }
}

And here is the scrip i attached to the flying enemy

void Start () {
        currentHp = maxHp;
}

public void RemoveHealth(float amount)
    {
        currentHp -= amount;
        healthBar.fillAmount = currentHp / maxHp;
        if (currentHp <= 0 && !hasDied)
        {
            hasDied = true;
            Destroy(gameObject);
            Instantiate(bloodParticle, transform.position, transform.rotation);
        }
    }

See, FindObjectOfType() will look for all GameObjects on scene, and first finded GameObject with MakeDead script will your find result.

It is wrong, because you need to kill/damage your target enemy. You can find your target enemy game object from your hit, by just using hit.collider.gameObject . And then just find in this game object your MakeDead script using GetComponent(), and call function RemoveHealth(damage);.

P.S i also cleared your code, please check if its works, because i don’t tested it.

Just change your code to it:


    void FireRocket()
    {
        Vector2 mousPos= Camera.main.ScreenToWorldPoint(Input.mousePosition);

        RaycastHit2D hit = Physics2D.Raycast(gunTip.position, mouPos - gunTip.position);
        if (hit.collider != null)
        {
            if (hit.collider.tag == "Flying Enemies")
            {
                hit.collider.gameObject.GetComponent<MakeDead>().RemoveHealth(damage);
            }
        }
    }