Applying damage to the player health problem

The player max health is at 100f . But I only want to take -5f . The problem I am having its taking to much health . When the enemy attacks the player health goes from 100f to -1045f very fast. I don’t know why. For example the player health should be 95f. Here is my attack script :

using UnityEngine;
using System.Collections;

public class attack1 : MonoBehaviour {
public GameObject player;
public GameObject bear;
public float distance;
private health2016 health2016Script;
public static bool isPlayerAlive = true;

// Use this for initialization
void Start () {




}

// Update is called once per frame
void Update () {
player = GameObject.FindWithTag("Player");
bear = GameObject.Find("bear");
if (isPlayerAlive)
{  

}

distance = Vector3.Distance(player.transform.position, bear.transform.position);

if (distance <= 3)
{	
	GameObject Player = GameObject.FindWithTag("Player");
    health2016Script = Player.GetComponent<health2016>();
	health2016Script.max_health -= 5f;
	GetComponent<Animation>().CrossFadeQueued("move" , 0.3f, QueueMode.PlayNow);
	GetComponent<Animation>().CrossFade("attack");
}

else if (distance < 15 && distance > 3.0f) {
	GetComponent<Animation>().CrossFadeQueued("attack", 0.3f, QueueMode.PlayNow);
	GetComponent<Animation>().CrossFade("move");
	transform.LookAt(player.transform.position);

	



}

}

}

and Player health script :

using UnityEngine;
using System.Collections;

public class health2016 : MonoBehaviour {

public float max_health = 100f;

void ApplyDamage()
{
	if (max_health <= 0f)
	{
		Destroy(gameObject);
	}
	
}

}

Hi There!

I believe what is happening is that your attack is happening every frame once the characters are within a certain distance of one another.

The below code that is reducing your characters health is in the Update() function which is called every single frame (Reference: Unity Video on Update Function ):

if (distance <= 3)
 {    
     GameObject Player = GameObject.FindWithTag("Player");
     health2016Script = Player.GetComponent<health2016>();
     health2016Script.max_health -= 5f;
     GetComponent<Animation>().CrossFadeQueued("move" , 0.3f, QueueMode.PlayNow);
     GetComponent<Animation>().CrossFade("attack");
 }

Additionally, this unity tutorial video on player health covers a good example better than I could. 7 minutes into the video is where he begins the discussion on how the damage system hits the health. He uses a variable called “damaged” within a TakeDamage function. Damaged is set to true during the TakeDamage function call, but damaged is also updated to “False” every single frame, so that the player does not take continuous damage every frame.

Hope this helps!