In my game I have a player with health and I am currently trying to add a healthbar but my one problem is that when my player loses hp my healthbar doesn’t change. In my scripts the script that updates the heathbar is linked to my health script but when my player loses hp the players hp doesn’t change and I am unsure why. The script for my healthbar:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class HealthBArScript : Health
{
private void Update()
{
transform.localScale = new Vector3(healthPoints / MaxHealth, 1.0f, 1.0f);
}
}
The script for my health if that is necessary:
using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
using UnityEngine;
public class Health : MonoBehaviour
{
public float healthPoints = 100.0f;
public float MaxHealth = 100.0f;
private Coroutine onHit = null;
private void Update()
{
StartCoroutine(Updaate());
}
IEnumerator Updaate()
{
if (healthPoints <= 0)
{
Destroy(gameObject);
}
if (healthPoints >= 101.0f)
{
healthPoints = 100.0f;
}
yield return new WaitForSeconds(2.0f);
}
private void OnTriggerEnter(Collider coll)
{
if (coll.gameObject.tag == "Zombie")
{
if (onHit == null)
onHit = StartCoroutine(HitDelay());
}
if(coll.gameObject.tag == "Health")
{
healthPoints = healthPoints + 10;
}
if (coll.gameObject.tag == "Health_1")
{
healthPoints = 100.0f;
}
}
IEnumerator HitDelay()
{
healthPoints = healthPoints - 25;
yield return new WaitForSeconds(2.0f);
onHit = null;
}
}