Better solution for this Script (Healthbar with Animation)?

Hey Guys,

I’m kinda new to Unity with C# and I wrote a Script for the Healthbar.
My Script looks untidy. Could anyone help me clean this mess up or someone who can explain to do that in a better way?

    //TopScreen GUI - Hearts
    public GameObject full_3;
    Animator full_3_anim;
    public GameObject full_2;
    Animator full_2_anim;
    public GameObject full_1;
    Animator full_1_anim;

    public GameObject half_3;
    Animator half_3_anim;
    public GameObject half_2;
    Animator half_2_anim;
    public GameObject half_1;
    Animator half_1_anim;

    public GameObject empty_3;
    public GameObject empty_2;
    public GameObject empty_1;

    public GameObject Player;
    public GameObject BottomCam;
    public GameObject GameoverCam;
   


    void Awake()
    {       
        full_3_anim = full_3.GetComponent<Animator> ();
        full_2_anim = full_2.GetComponent<Animator> ();
        full_1_anim = full_1.GetComponent<Animator> ();
        half_3_anim = half_3.GetComponent<Animator> ();
        half_2_anim = half_2.GetComponent<Animator> ();
        half_1_anim = half_1.GetComponent<Animator> ();
        full_3_anim.speed = 0.33f;
        full_3_anim.Play("Beat");
    }
//----------------------------------------------------------------
    IEnumerator GetDamage() {
        Debug.Log ("Damage Got");       
        if(Life == 1){
            Life = Life-1;
            half_1.SetActive(false);
            empty_1.SetActive(true);
            Player.SetActive(false);
            BottomCam.SetActive(false);
            GameoverCam.SetActive(true);
            Application.LoadLevelAsync("BootScreen");
        } else if(Life == 2){
            Life = Life-1;
            full_1.SetActive(false);
            half_1.SetActive(true);
            half_1_anim.speed = 0.66f;
            half_1_anim.Play("Beat");
            yield return new WaitForSeconds(0);
        } else if(Life == 3){
            Life = Life-1;
            half_2.SetActive(false);
            empty_2.SetActive(true);
            full_1_anim.speed = 0.48f;
            full_1_anim.Play("Beat");
            yield return new WaitForSeconds(0);
        } else if(Life == 4){
            Life = Life-1;
            full_2.SetActive(false);
            half_2.SetActive(true);
            half_2_anim.speed = 0.33f;
            half_2_anim.Play("Beat");
            yield return new WaitForSeconds(0);
        } else if(Life == 5){
            Life = Life-1;
            full_2_anim.speed = 0.33f;
            full_2_anim.Play("Beat");
            half_3.SetActive(false);
            empty_3.SetActive(true);
            yield return new WaitForSeconds(0);
        } else if(Life == 6){
            Life = Life-1;
            full_3.SetActive(false);
            half_3.SetActive(true);
            half_3_anim.speed = 0.33f;
            half_3_anim.Play("Beat");
            yield return new WaitForSeconds(0);

        }
        Debug.Log (Life + " left");
    }

First of all - get rid of the GameObject declarations and just assign the Animators via the Inspector. You can get back to the GameObject with the .gameObject property. So that gets rid of half your variables :slight_smile:

Secondly, you could probably put these in an array where each animators index in the array corresponds to the amount of health that “activates” it. Then you could do something like

half[Life].SetActive(false);

and skip all your if statements.

Also - depending on what you’re specifically doing you might be able to use a Slider for this.

Will try it. A Example Script as reference (or my one modified) would be better to understand how this works.

With my script, the Healthbar looks like this:

stop trying to do everything in one script, break things down into smaller parts.

Looking at that gif.

The hearts are all duplicates of each other. They have 3 states, beating, static and dead; and some sort of fill value (boolean or float whichever you need).
Make one heart with a script which makes that all happen working on the current state and current fill value. This becomes a prefab.

Next you want multiple hearts to be used in a healthbar. Script that, it just tracks the current value, max value etc. and instantiates and arranges hearts as needed in a collection and sets their states and fill values based on the current health value.

Bonus is that since this approach is based around a collection (i.e. list) you can have any number of hearts without having to rework your hardcoded scripts.

2 Likes

Sounds good.
Have to find out now how to do that in the best way.
As I already said I’m still a “Rookie” in C# ^^’