- Home /
Unanswered and off topic just wanted to know how to get two sliders working together.
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;
}
Answer by Mehul-Rughani · Nov 29, 2016 at 12:24 PM
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:)
@$$anonymous$$ehul-Rughani Could you give me an example of number one? I am not using images like the heart icons.
Reference image that you are using for progress bar
Im not using images. I'm using four ui sliders, and not sure how to reference each one
Answer by Eepiz · Nov 30, 2016 at 07:38 AM
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 :)
Answer by Wolfie · Dec 01, 2016 at 01:01 AM
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.
The rad began in an awkward cut with the image being a diamond otherwise I would have stuck with that. It Began and end on the midway of the first section. I tried the other settings but it did not look right.
I think you've lost me, can you post a screenshot? It sounds like you've just started out with a square and then rotated it to make it a diamond. The radial fill doesn't take your own rotation into account, so you could try taking your image, rotating it 45 degrees in Photoshop or whatever you use, and then trying the same thing again.
Got ya, make sure its already 45 before import
Follow this Question
Related Questions
Script updates prefab but not instantiated clone of prefab 0 Answers
How to show the change in slider value 1 Answer
Dynamic slider size with the new UI 2 Answers
left end of slider fill flattening the more i move the value down 0 Answers
Why hip will move upward and downward automatically when start to drag slider at x,y and z axis? 0 Answers