Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Health slider not working

Discussion in 'Scripting' started by Zainu, Jan 27, 2015.

  1. Zainu

    Zainu

    Joined:
    Jan 27, 2015
    Posts:
    5
    I have a health slider at the bottom of my screen.

    • I have a player (prefabs and is only instantiate after the user pressed 'start game button')
    • Player prefabs has 'PlayerHealth script' with Health slider linked to it.
    • I have healthslider as (prefabs) since I need to link it to Player(prefabs) under Player health(script) in the Player Inspector .


    Code (JavaScript):
    1. function OnControllerColliderHit(theCollision : ControllerColliderHit){
    2.      if (theCollision.gameObject.name == "Palm"){
    3.         Debug.Log("OnControllerColliderHit detected Palm OnControllerColliderHit");
    4.         if(isColliderHit){
    5.           TakeDamage(50);
    6.           isColliderHit = false;
    7.        }
    8.      }
    9. }
    10.  
    11.  
    12. public function TakeDamage (amount : int)
    13. {
    14.     // Set the damaged flag so the screen will flash.
    15.     damaged = true;
    16.  
    17.     // Reduce the current health by the damage amount.
    18.     currentHealth -= amount;
    19.  
    20.     // Set the health bar's value to the current health.
    21.     healthSlider.value = currentHealth;
    22.  
    23.     Debug.Log("currentHealth: " +currentHealth);
    The Debug log prints out the correct currentHealth but the HealthSlider remains full at 100%. Any help please...
     
    Last edited: Jan 27, 2015
  2. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    What sort of thing is HealthSlider? That's not a built-in class as far as I'm aware. Where did you get it, or what code does it contain, that should update some sort of display when you change the .value property?
     
  3. Zainu

    Zainu

    Joined:
    Jan 27, 2015
    Posts:
    5
    public function TakeDamage (amount : int)
    {
    // Set the damaged flag so the screen will flash.
    damaged = true;

    // Reduce the current health by the damage amount.
    currentHealth -= amount;

    // Set the health bar's value to the current health.
    healthSlider.value = currentHealth;

    Debug.Log("currentHealth: " +currentHealth);
    I'm using unity component Slider to make my healthslider. Im following this tutorial but couldnt get mine working. http://unity3d.com/learn/tutorials/projects/survival-shooter/player-health

    The only diff is the tutorial uses Healthslider when Im using Healthslider inside a prefab. Is this the issue?
     
    Last edited: Jan 28, 2015
  4. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    I don't think so. A prefab is just a game object that's not instantiated in the scene until you Instantiate it (which can be done at startup, if you drag it into your hierarchy). Once instantiated, it's no different from any other object.

    I have to confess I'm stumped. But if you can strip your project down to a simple example and post it somewhere, I'll have a look.
     
  5. Zainu

    Zainu

    Joined:
    Jan 27, 2015
    Posts:
    5
  6. Zainu

    Zainu

    Joined:
    Jan 27, 2015
    Posts:
    5
    Did I miss out something in the test sample for the health slider?
     
  7. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    Well, I had a little trouble figuring out how to run it. It appears to still be a client/server app; a stand-alone test would be much better.

    But I think I reproduced the problem this way: I edited the PlayerHealth.js script so that it always does "healthSlider.value = currentHealth;" in the Update event.

    Then I ran the game, and dragged a PlayerPrefab out into the scene. I see that you have PlayerPrefab set up with a Slider slot that's hooked up to HealthSliderPrefab... which has no relation to the health slider actually on the screen.

    So, when you change the PlayerHealth's health value, it does indeed update the .value of the HealthSliderPrefab. And it doesn't update the one on screen.

    If you're going to have the health slider on screen from the beginning, and then instantiate the PlayerPrefab later, then I think you need to dynamically find the slider (e.g. in PlayerHealth.Start) by using something like GameObject.Find.

    (I tried that, but was unable to quickly figure out how to typecast in JavaScript!)

    Good luck,
    - Joe
     
  8. Zainu

    Zainu

    Joined:
    Jan 27, 2015
    Posts:
    5
    Thanks Joe, it worked!

    Code (JavaScript):
    1. //Declare
    2. private var guiHealthBar : GameObject;
    3. private var healthBarNewScript: Slider;
    4.  
    5. //Inside function Awake.
    6. guiHealthBar = GameObject.FindGameObjectWithTag ("HealthSlider");
    7. healthBarNewScript = guiHealthBar.GetComponent (Slider);
    8.  
    9. //Updating the healthbar value, anywhere you want it to be
    10. healthBarNewScript.value = currentHealth;
     
    JoeStrout likes this.
  9. Hemalab

    Hemalab

    Joined:
    Feb 6, 2016
    Posts:
    5
    any chance i can get this code in c#? having the same problem with my HealthSlider



    using UnityEngine;
    using UnityEngine.UI;
    using System.Collections;
    using UnityEngine.SceneManagement;
    using UnityEngine.Networking;


    public class PlayerHealth : NetworkBehaviour
    {
    public int startingHealth = 100;
    public int currentHealth;
    public Slider healthSlider;
    public Image damageImage;
    public float flashSpeed = 5f;
    public Color flashColour = new Color(1f, 0f, 0f, 0.1f);



    Animator anim;

    bool isDead;
    bool damaged;


    void Awake ()
    {
    anim = GetComponent <Animator> ();
    currentHealth = startingHealth;
    currentHealth = startingHealth;
    }



    public void TakeDamage (int amount)
    {
    damaged = true;

    currentHealth -= amount;

    healthSlider.value = currentHealth;

    if(currentHealth <= 0 && !isDead)
    {
    Death ();
    }
    }


    void Death ()
    {
    isDead = true;



    anim.SetTrigger ("Die");



    }


    public void RestartLevel ()
    {
    SceneManager.LoadScene (0);
    }
    }
     
  10. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    Please use code tags when posting code.

    And it seems to me that you already have that code in C#. What problem are you having, exactly?