What's wrong with this health bar script?

Hello,
Why does my healthbar full and doesn’t work?
Is it something wrong with my script?

Thanks :slight_smile:
Error:

transform.localScale assign attempt for 'Bar' is not valid. Input localScale is { NaN, 1.000000, 1.000000 }.
UnityEngine.Transform:set_localScale(Vector3)
EnemyLogic:setHealthbar(Single) (at Assets/Scripts/EnemyLogic.cs:28)
EnemyLogic:ApplyDamage(Int32) (at Assets/Scripts/EnemyLogic.cs:15)
UnityEngine.Component:SendMessage(String, Object, SendMessageOptions)
MeleeSystem:Update() (at Assets/Scripts/MeleeSystem.cs:16)

Script::

using UnityEngine;
using System.Collections;

public class EnemyLogic : MonoBehaviour {
   
    public float max_Health = 100f;
    public float cur_health = 0f;
    public GameObject Bar;
   

    public void ApplyDamage (int Damage){
        max_Health -= Damage;
        cur_health = max_Health;
        float calc_Health = cur_health / max_Health;
        setHealthbar (calc_Health);
        if (max_Health <= 0) {
            Dead();

        }
    }
   
    private void Dead(){
        Destroy (this.gameObject);
    }
   
    public void setHealthbar(float enemyHealth) {
        //enemyHealth is 0-1, calculated based on health and max health and current health :P
        Bar.transform.localScale = new Vector3(Mathf.Clamp(enemyHealth,0f ,1f), Bar.transform.localScale.y, Bar.transform.localScale.z);
    }
}

The logic of ApplyDamage seems a bit funny to me. Why lower max health when you take damage? You should be lowering current health, and keeping max where it is, so you can get the proper percentage to fill your hp bar.

Right now it looks like these lines are the culprit:

cur_health = max_Health;
float calc_Health = cur_health / max_Health;

You are always going to have calc_result return 1, making your health bar stay full. The exception is if max_Health drops to 0 from your ApplyDamage method, in which case you will divide by zero. When you pass try to set the local scale with that result, you get the error you are seeing.

NaN means ‘Not a Number’, which can result from the result of an operation being undefined (like when you divide by zero).

Yes, that seems about right, so what can i do to have this work correctly?

Operating on currentHealth instead of Max should solve this bug, assuming the rest of your scripts are working.

cur_health -= Damage;
float calc_Health = cur_health / max_Health;
setHealthbar (calc_Health);

So what should my script look like? What should i change?

Bump

This should work

using UnityEngine;
using System.Collections;
public class EnemyLogic : MonoBehaviour {

    public float max_Health = 100f;
    public float cur_health = 100f;
    public GameObject Bar;

    public void ApplyDamage (int Damage){
        cur_health -= Damage;
        float calc_Health = cur_health / max_Health;
        setHealthbar (calc_Health);
        if (max_Health <= 0) {
            Dead();
        }
    }

    private void Dead(){
        Destroy (this.gameObject);
    }

    public void setHealthbar(float enemyHealth) {
        //enemyHealth is 0-1, calculated based on health and max health and current health :P
        Bar.transform.localScale = new Vector3(Mathf.Clamp(enemyHealth,0f ,1f), Bar.transform.localScale.y, Bar.transform.localScale.z);
    }
}

Well now my unity wont die. Healthbar works and goes down but the units health doesn’t do anything when it gets to 0, it just keeps going down into negatives?

You’re checking to see if max health is <= 0. Should be checking cur health.

Thanks :smile: