public GameObject damage;
public float damageTaken;
public GameObject EnemyHP;
public int EnemyHealth;
public TMP_Text EHP;
public GameObject EnemyHealthHP;
public float MaxHP;
public UnityEngine.UI.Button hit;
public GameObject load;
public float currentHP;
public float resize;
private void Start()
{
}
// Update is called once per frame
void Update()
{
if (load.GetComponent<Attack>().enemyload == true)
{
Debug.Log("ENEMYLOAD");
MaxHP = load.GetComponent<Attack>().EnemyMaxHealth;
EnemyHealth = 200;
EHP.text = MaxHP + "/" + MaxHP;
load.GetComponent<Attack>().enemyload = false;
currentHP = MaxHP;
}
hit.onClick.AddListener(Attack);
}
void Attack()
{
if (load.GetComponent<Attack>().dmgload == true)
{
Debug.Log("EnemyDMG");
damageTaken = damage.GetComponent<Attack>().damage;
Debug.Log(damageTaken);
currentHP -= damageTaken;
resize = currentHP / MaxHP;
RectTransform EnemyHP = GetComponent<RectTransform>();
EnemyHP.sizeDelta = new Vector2(EnemyHealth*resize, EnemyHP.sizeDelta.y);
EHP.text = currentHP + "/" + MaxHP;
load.GetComponent<Attack>().dmgload = false;
}
This is the code for controlling the enemy health in my game. It works perfectly the first time the attack button is pressed, but the second time the amount of damage that is supposed to be dealt doubles. Lets say damageTaken is 10 and the maxHP is 20. After the first attack currentHP is 10, however after the second attack currentHP becomes -10, on the third it becomes -50, and so on. damageTaken doesn’t change during this and does stay 10 every time, however current HP takes double what it did the previous time for reasons unknown to me. Everything else is currently working except currentHP of the enemy and player (both player and enemy scripts are nearly identical other than the variables they are taking from the script and the script controlling player has a coroutine to wait till the enemy deals damage). Any help is greatly appreciated!!