Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

Health in UI not updating when player dies

Discussion in 'Scripting' started by Truna_, Dec 3, 2018.

  1. Truna_

    Truna_

    Joined:
    Nov 14, 2018
    Posts:
    3
    Hi, I'm very new to C# and programming in general but I've been trying to make a basic platformer. However, the UI that I just added to the script won't update the player's health to 0 when the player's gameObject is destroyed and the gameOver panel pops up. It just remains at whatever health the player has before the player dies.

    Also, as a side question, I've been trying to use Debug.Log(health); to show me what the health is every time it's being updated, but for some reason health isn't showing up in the debugger? Are there specific places in code where I'm limited to using this? I clicked all three of the error types on the right of the console and the debugger is working for my score variable in another script, so I'm really confused.

    I've been following simple 2D tutorials from Blackthornprod and Brackeys for most of the code. I've been stuck on this same bug for hours, so any help would be greatly appreciated. Thanks in advance!

    Code (CSharp):
    1. public class Player : MonoBehaviour {
    2.     // player health
    3.     public int health = 3;
    4.  
    5.     // establishing a fall boundary
    6.     private int fallBoundary = -20;
    7.  
    8.     // object to display game over panel
    9.     public GameObject gameOver;
    10.  
    11.     // effect to be used when game is over
    12.     public GameObject obstacleEffect;
    13.  
    14.     // variable to display health
    15.     public Text healthDisplay;
    16.  
    17.     private void Update () {
    18.         // display health by converting to string
    19.         healthDisplay.text = health.ToString();
    20.  
    21.         // damage player a lot if player falls
    22.         if (transform.position.y <= fallBoundary){
    23.             DamagePlayer(health);
    24.         }
    25.     }
    26.  
    27.     // player dies if health goes below 0
    28.     public void DamagePlayer(int damage) {
    29.         health -= damage;
    30.         // kill player by destroying game object
    31.         if ((health <= 0) && (GameObject.FindGameObjectWithTag("Player") != null)) {
    32.             gameOver.SetActive(true);
    33.             Destroy(gameObject);
    34.         }
    35.     }
    36. }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,599
    The console window has a few colored buttons in the upper right corner, one for log, warning and error. Make sure they are enabled otherwise that type of message will be hidden.

    If those are enabled, then the likely explanation is that your code is not actually being called.
     
  3. Truna_

    Truna_

    Joined:
    Nov 14, 2018
    Posts:
    3
    Thanks for the reply!
    I enabled all three of those buttons. The code apparently is only being called when the player hasn't died yet, which is bizarre to me.
     
  4. Truna_

    Truna_

    Joined:
    Nov 14, 2018
    Posts:
    3
    Editing to say I fixed this, I feel so stupid now. I moved
    healthDisplay.text = health.ToString(); 
    after
    if (transform.position.y...
    , and I also added a second
    healthDisplay.text = health.ToString(); 
    after
    gameOver.SetActive(true);
    .
     
    Kurt-Dekker likes this.