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.

1 Reply

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.