How to connect multiple sliders for health bar?

How do I connect multiple sliders to act as one in a script. In the picture below I have the sliders laid out in the pattern it should go through. Slider 1 = 100 and ends the rotation out at slider 4 = 0. I am not very good with arrays yet. Just need to know how to make the connection from 1 to 2 without both health bars going down at the same time. Do I need to do anything in the onValueChanged? Didn’t have to mess with that on a single so I am not sure. Thank you !!

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

public class PlayerHealth : MonoBehaviour 
{
	public int curHealth = 100;

	public Slider healthSlider1;
	public Slider healthSlider2;
	public Slider healthSlider3;
	public Slider healthSlider4;


	void Start()
	{
		
	}
		
	void Update()
	{
		healthSlider1.value = curHealth;
	}

	//UI Button to test damage
	public void TestDamage()
	{
		curHealth = curHealth - 5;
	}

Hey @HavenGaming
You can do it in two ways

  1. you can do it manually.by enabling and disabling pieces of progress bar
    for example : when health decrease 25 disable last object and when it increase 25 enable last object of progress bar

  2. make an image looks like a four slides and add that image as ui image in scene and set image type to fill and set fill amount according to your health

Hope you are looking for this:)

You could put your sliders in an array, set the min value of the first slider to 0 and the max value to 25, min value of the second slider to 25 and max value to 50 and so on.
When setting a new health, iterate over all sliders and change their value to the new health. If the value is smaller than a sliders min value it will take it’s min value, if it’s bigger it will take it’s max value.

public Slider[] sliders;

void SetHealth(int health) {
	foreach (Slider slider in sliders) {
		slider.value = health;
	}
}

I hope this helps :slight_smile:

Looking at your GUI screenshot, another option to consider could be to treat it like a circular health bar. Create an image component with your square bars and a transparent background.

Image type - “filled”
Fill method - “Radial360”

Then change the “fill amount” variable by code, multiplying so as to convert your percentage health or arbitrary health units into an angle.

Besides being simple to do, one reason I suggest this is that many other methods are liable to have issues at the corners or your square health bar, which while not game breaking, would still be noticable and would necessitate some trickier work-arounds. If you want to retain the dark border whilst cutting into the health bar, then simply put that part of the graphic on a layer above the bar itself.