Hello, been scripting some Unity for a while now and doing a University Project for it (just starting).
I have a script called Health, on the player controller. It has a float called HP and a method called getHP() to return this float.
The problem I am having is in my HUD script I am trying to display this value, now it works fine if I use the buttons (code you will see below) to damage the player, but when an outside source hurts me (zombies woo!) the HUD does not update, even through I grabbed the Health at the start and are using the public method to access the value of the private float.
See code below.
//CODE Starts
//HUD Script
using UnityEngine;
using System.Collections;
public class HUD : MonoBehaviour {
Health _Health;
public float HP = 0f;
public float Amount = 10f;
public Texture2D DeadScreen;
//---------------------------------------------
// Use this for initialization
void Start () {
_Health = gameObject.GetComponent<Health>();
HP = _Health.GetHP();
}
//---------------------------------------------
// Update is called once per frame
void Update () {
_Health = gameObject.GetComponent<Health>();
if(_Health){
Debug.Log("exists");//runs every time
}else{
Debug.Log("doesnt exist");//never runs
}
HP = _Health.GetHP();
Debug.Log ("GUI HP Update: "+HP);
//My attempt to update the value by regrabbing the component
}
//---------------------------------------------
void OnGUI(){
float Y = 5;
if( !_Health.GetAlive() ){
GUI.DrawTexture(new Rect(0, 0, Screen.width, Screen.height), DeadScreen, ScaleMode.StretchToFill, true, 10.0F);
}
GUI.Box( new Rect( 5, Y, 100, 20), "HUD" ); Y+=20;
GUI.Label( new Rect( 5, Y,100, 20), HP.ToString() );
Debug.Log ("GUI HP: "+HP.ToString() );
GUI.Label (new Rect(110,Y, 40, 20), Percentage ( _Health.GetHP(), _Health.Max_HP).ToString("0")+"%" );
Y=5;//redefine YCord
if(GUI.Button(new Rect(Screen.width-70 ,Y,30,20), "+")) {
Amount += 10;
}//if X, Y, W, H
if(GUI.Button(new Rect(Screen.width-35 ,Y,30,20), "-")) {
Amount -= 10;
}//if
Y+=25;
// X, Y, W, H
if(GUI.Button(new Rect(Screen.width-95 ,Y,90,20), "Heal "+Amount)) {
_Health.Healed(Amount);
}
Y+=20;
if(GUI.Button(new Rect(Screen.width-95 ,Y,90,20), "Damage "+Amount)) {
_Health.Damaged(Amount);
}
}//OnGUI
//---------------------------------------------
float Percentage( float Cur, float Max ){
return( (Cur/Max) * 100 );
}//Percentage
}//CLASS
//Health Script
using UnityEngine;
using System.Collections;
public class Health : MonoBehaviour {
public GameObject Replacement;
public float Max_HP = 100;
private float HP;
float priorHP;
bool Alive;
public bool AllowAboveMaxHP; //allow above max?
public bool DecreaseAboveMax; //decrease to Max?
public float DecreaseAmount= 1f; //how much
public float DecreaseTime = 2f; //how often to decrease
public bool AllowPassiveRecover; //increase to Max?
public float IncreaseAmount= 1f; //how much
public float IncreaseTime= 2f; //how often to increase
float TimeSince = 0; //Time since +/-
//---------------------------------------------
// Use this for initialization
void Start () {
HP = Max_HP;
Alive = true;
}//Start
//---------------------------------------------
// Update is called once per frame
void Update () {
Debug.Log (gameObject.name+" HP: "+HP);
if( !Alive ){
Destroy (gameObject, 3f);
}//if
if( HP == Max_HP ){
TimeSince = 0;
}//if back to Max, reset time frame
//if(AboveMaxHP){
if( Max_HP < HP){
if(AllowAboveMaxHP && DecreaseAboveMax){ //Reduce from AboveMax+?
TimeSince+=Time.deltaTime;
if( DecreaseTime <= TimeSince ){
TimeSince = 0;
HP -= DecreaseAmount;
if( HP < Max_HP){
HP = Max_HP;
}//if HP to Max if went under
}//if, time since
}else//if, decrease to max from above
if( !AllowAboveMaxHP ){
HP = Max_HP;
}//if HP should not be above max
}else//
if( HP < Max_HP && AllowPassiveRecover){ //Recover HP?
TimeSince+=Time.deltaTime;
if( IncreaseTime <= TimeSince ){
TimeSince=0;
HP+=IncreaseAmount;
if(Max_HP < HP){
HP=Max_HP;
}//if
}//if, time to passive recover
}//if, below Max and recover
//Debug.Log( gameObject.name+" gained: "+(HP-priorHP)+" Health.");
}//Update
//---------------------------------------------
public void Damaged( float Dam ){
TimeSince=0;
HP -= Dam;
if( HP <= 0 ){
Die();
}//if
Debug.Log( gameObject.name+" took: "+Dam+" Damage.");
}//Damaged
//---------------------------------------------
public void Healed( float Healing ){
priorHP = HP;
HP += Healing;
if(Max_HP < HP && !AllowAboveMaxHP){//HP becomes too big
HP = Max_HP;
Debug.Log( gameObject.name+" gained: "+(HP-priorHP)+" Health.");
}else{//Cap HP
Debug.Log( gameObject.name+" gained: "+Healing+" Health.");
}//HP increased
}//Damaged
//---------------------------------------------
void Die(){
Alive = false;
if( Replacement != null ){
Instantiate( Replacement, gameObject.transform.position, Quaternion.identity);
Destroy( gameObject );
}//if
}//Die
//---------------------------------------------
public float GetHP(){
return(HP);
}//GetHP
//---------------------------------------------
public bool GetAlive(){
return(Alive);
}//GetHP
}//CLASS
//CODE Ends
Thanks for any help or advice, the core issue is that OnGUI doesn’t seem to update or re-run when I get hurt, and even using the buttons to add or remove health doesnt update to the correct value, I can be on 10 HP and take away 8, and be on 92 as far as the HUD display is concerned.