Search Unity

Health bar UI not working after building.

Discussion in 'UIElements' started by Creater2004, May 22, 2019.

Thread Status:
Not open for further replies.
  1. Creater2004

    Creater2004

    Joined:
    Mar 27, 2019
    Posts:
    5
    I have created a health bar that works in editor, but once I build my game, it just does not decrease when I take damage. (I can take damage, but the health bar is not showing it.)
    (Pictures of inspector of health bar and hierarchy attached.)

    Here is the health bar script:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class HealthBarHUD : MonoBehaviour {
    7.  
    8.     public Image full;
    9.     public Image mid;
    10.     public PlayerHealthSystem phs;
    11.  
    12.     public float midPercent = 0.5f;
    13.     public float dangerPercent = 0.15f;
    14.     public float lowPercent = 0.3f;
    15.  
    16.     public PlayerHealth player;
    17.     // Use this for initialization
    18.     void Start () {
    19.  
    20.         player = GameObject.FindGameObjectWithTag("Play").GetComponent<PlayerHealth>();
    21.  
    22.         if ((phs.health/player.p_max_health) < this.midPercent)
    23.         {
    24.             full.fillAmount = 0;
    25.             mid.fillAmount = (2*phs.health ) / player.p_max_health;
    26.         }
    27.         else
    28.         {
    29.             mid.fillAmount = 1;
    30.             full.fillAmount = (phs.health / player.p_max_health);
    31.         }
    32.        
    33.     }
    34.    
    35.     // Update is called once per frame
    36.     void Update () {
    37.         if ((phs.health / player.p_max_health) < this.midPercent)
    38.         {
    39.             mid.fillAmount = (2*phs.health) / player.p_max_health;
    40.             full.fillAmount = 0;
    41.         }
    42.         else
    43.         {
    44.             mid.fillAmount = 1;
    45.             full.fillAmount = (phs.health / player.p_max_health);
    46.         }
    47.     }
    48. }
    49.  
    HealthSystem (To convert current health into "health" variable):

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerHealthSystem : MonoBehaviour {
    6.     public float health;
    7.     public PlayerHealth player;
    8.  
    9.     // Use this for initialization
    10.     void Start () {
    11.         player = GameObject.FindGameObjectWithTag("Play").GetComponent<PlayerHealth>();
    12.     }
    13.    
    14.     // Update is called once per frame
    15.     void Update ()
    16.     {
    17.         health = player.p_curHealth;
    18.     }
    19. }
    20.  
    Is this caused by a coding error, or other reasons? Thanks.
     

    Attached Files:

  2. antoine-unity

    antoine-unity

    Unity Technologies

    Joined:
    Sep 10, 2015
    Posts:
    101
Thread Status:
Not open for further replies.