Count number of times... animation?

Hi guys, I am working on a melee combat system for my game, it works detecting the distance of the enemy from the player, if the enemy is really close it triggers the attack animation,
at the same time the healthbar decreases based on the distance, the problem is that it does not look realistic because the attack animation and the way the healthbar decreases is not synchronized, so I am going to decrease my player animation based on the number of times the attack animation plays, but how can I do this, any ideas?

it would be something like:

if(animationPlayed == 10){decrease 10 points;}


Have you heard of Animation Events? You can add them in the animation window. This problem can easily be solved with these events:

  1. Create a script with a function which does one thing - add one to a variable (like animationPlayed). Attach this script to the object which has the animation
  2. Create an Animation Event which calls for the function from the new script. This event should be on the last frame of the animation.
  3. Call for the variable in another script, just like you already are. It would look like (GetComponent(/name of script/).animationPlayed).