I am currently using RayCastHit to attack the enemy player and in my game it is showing that the enemy is being attacked in console window but the enemy health will not decrease. I have used similar code for enemy attacking the player and it does decrease the health. Please Help if you know any solution to this problem, I cannot figure it why it does not work. I attached the images of the code.
,
GameObject.FindWithTag()
will return only one element, so unless you only have one enemy in the scene at a time, is likely not what you want here.
GameObject.FindWithTag
If EnemyStats is attached to the same GameObject as the collider, then you can use
hit.collider.GetComponent<EnemyStats>()
to get the raycasted object. If you need to be sure it has the tag, then you can use GameObject.tag to check it.