Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Yinoguns · Oct 02, 2013 at 01:42 PM · guigetcomponentdamagehudhp

Health & HUD Script, not syncing

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Yinoguns · Oct 02, 2013 at 02:21 PM

Found the issue, my Zombie was attacking the Root of the player; an Empty, because it had the Player Tag.

Of course the HUD and Health script of the player are on the capsule collider, as then in future making additions like fall damage would be easier.

Sorry for the space.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

14 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

GUI Update 1 Answer

Where can I find the scripts used in the Player State Machine & HUD/GUI Tutorials? 1 Answer

Cameras Switch Based On Button Click 1 Answer

'thrust' is not a member of UnityEngine.Component 1 Answer

creation of buttons and other gui stuffs 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges