- Home /
Why does my player health not change when I want it to
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;
}
}
Answer by AryaIsMe · May 12, 2021 at 08:40 PM
I was able to fix it by changing the script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class HealthBArScript : MonoBehaviour
{
public Health trackedHealth;
void Update()
{
transform.localScale = new Vector3(trackedHealth.healthPoints / 100, 1, 1);
}
}
All I had to do was drag the player on as the trackedHealth in the inspector
Answer by sonicismael2 · May 08, 2021 at 08:53 AM
Your 'MaxHealth' variable isn't being updated, or being used at all in the Health script... the variable I think you want to be referencing in your Health Bar script should be your 'healthPoints' variable which is the one that is changing in the health script. 'MaxHealth' would imply a constant, probably why you aren't updating it, and thus why the healthbar's dimensions aren't changing.
public class HealthBArScript : Health
{
private void Update()
{
transform.localScale = new Vector3(healthPoints / healthPoints, 1.0f, 1.0f);
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613033442im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Passing variables between my Visual Studio website (just local) and Unity 0 Answers
Visual Studio 2010 Professional, Missing System.Web assembly reference 0 Answers
Visual Studio 2010 causing: Inconsistent Line Endings... 3 Answers