Problem With Multiple Sliders - Health Bars

Hi everyone, I’m a old Cobol programmer, trying to learn unity for a life change, and got stuck with this issue, already looked up on many posts around here and on stackoverflow, but nothing so far gave me at least an idea of what I’m doing wrong.

I’m working on a little 2D Sidescrolling SHMUP, and I am having a little trouble with Health Bars(I’ve been following a few online tutorials, both from Unity and from Youtube).

What I’m trying to accomplish: I have a scene with a boss battle, and during this scene, I want to display two Health bars onscreen, one for the boss and one for the player.

What I’m getting: When I create the first Slider Bar, and create the code for it, it works normally. However, when I create the second Slider bar, I get either of the following results: None of the bars Work, or both the boss and the player cause damage on the first bar.

Here’s what I’ve done: In the scene, I’ve created a Canvas, as a child of the canvas, I’ve created an Empty gameObject that I’ve called HUD. As childs of this HUD object, I’ve created the two Slider UI elements.
Like this:
79503-hiearchy.png

After that I’ve created an Script to control the sliders, and attached it to the HUD Object.
the code is as follows:

using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class HudControl : MonoBehaviour {

    public PlayerBF playerBf;
    public BossShotControl boss;
    public Slider playerSlider;
    public Slider bossSlider;
    private int playerPrevHp, bossPrevHp;
    // Use this for initialization
	void Start () {
        playerBf = GetComponent<PlayerBF>();
        boss = GetComponent<BossShotControl>();
        playerSlider = GetComponent<Slider>();
        bossSlider = GetComponent<Slider>();
        playerPrevHp = playerBf.hp;
        bossPrevHp = boss.bossHp;
	}
	
	// Update is called once per frame
	void Update () {
        if (playerBf.hp != playerPrevHp)
        {
            playerSlider.value -= 1;
            playerPrevHp = playerBf.hp;
        }
        if (boss.bossHp != bossPrevHp)
        {
            bossSlider.value -= 1;
            bossPrevHp = boss.bossHp;
        }
	}
}

I’ve also tried changing, the “GetComponent<>()” for (PlayerBF)FindObjectOfType(typeof(PlayerBF));
but to the same results.
I’ve also tried making reference to the sliders inside of the scripts of the bullets(bossbullets and playerbullets), and make them decrease at the same time I cause damage, but again, the results were the same, only one slider taking damage, or both not working.

Thanks in Advance, any help will be appreciated.

Try this approach

using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class HudControl : MonoBehaviour
{
    public Slider playerSlider;
    public Slider bossSlider;

    private int playerPrevHp, bossPrevHp;

    // Use this for initialization
    void Start ()
    {
        ResetValues();
    }

    void ResetValues ()
    {
        playerSlider.maxValue = 100;
        playerSlider.minValue = 0;
        playerSlider.value = 100;            // starts a 100 goes down to 0

        bossSlider.maxValue = 100;
        bossSlider.minValue = 0;
        bossSlider.value = 100;              // starts a 100 goes down to 0

        playerBf.hp = playerSlider.maxValue; // needs to start out at the same value as playerSlider.maxValue
        boss.bossHp = bossSlider.maxValue;   // needs to start out at the same value as bossSlider.maxValue
        
        playerPrevHp = 100;                  // needs to start out at the same value as playerSlider.maxValue
        bossPrevHp = 100;                    // needs to start out at the same value as bossSlider.maxValue
    }

    // Update is called once per frame
    void Update ()
    {
        if (playerBf.hp != playerPrevHp)
        {
            playerSlider.value = playerBf.hp;
            playerPrevHp = playerBf.hp;
        }

        if (boss.bossHp != bossPrevHp)
        {
            bossSlider.value = boss.bossHp;
            bossPrevHp = boss.bossHp;
        }
    }
}

Please let me know if you have any questions