- Home /
Unity 4.6 How to change slider color via script
Hi, I am trying to make a sort of health bar with a slider from the new UI system. I wanted to get fancy and have the slider background color start as green and then change to red once the health is below a certain threshold. However I cannot figure out how to change the background color of the Fill Area of the slider via code. I did some googling but no luck so far.
Thanks!
Answer by Mexallon · Dec 23, 2014 at 07:25 PM
What you want is to change the Color of the Image component of the "Background" Child of the Slider and set it to red. And the Slider->Fill Area->Fill Child has also an Image Compinent with an Attribute called Color that you want to set to green for example.
You could use Color.Lerp if you want to fade between green and red as the health goes down.
I've tried it out this is an example for the use of Color.Lerp to get a smooth change from red health bar to green as I want it but you are free to change it that it only changes it color if its below 1/3 health or similar:
public class HealthBar : $$anonymous$$onoBehaviour {
private Slider slider;
private int counter;
public int $$anonymous$$axHealth = 500;
public Image Fill; // assign in the editor the "Fill"
public Color $$anonymous$$axHealthColor = Color.green;
public Color $$anonymous$$inHealthColor = Color.red;
private void Awake() {
slider = gameObject.GetComponent<Slider>();
counter = $$anonymous$$axHealth; // just for testing purposes
}
private void Start() {
slider.wholeNumbers = true; // I dont want 3.543 Health but 3 or 4
slider.$$anonymous$$Value = 0f;
slider.maxValue = $$anonymous$$axHealth;
slider.value = $$anonymous$$axHealth; // start with full health
}
private void Update() {
UpdateHealthBar(counter); // just for testing purposes
counter--; // just for testing purposes
}
public void UpdateHealthBar(int val) {
slider.value = val;
Fill.color = Color.Lerp($$anonymous$$inHealthColor, $$anonymous$$axHealthColor, (float)val / $$anonymous$$axHealth);
}
}
Thanks a lot! Will try this. I didn't realize you could assign child objects like the Fill to a public variable.
One question though. I though the slider value only goes between 0 and 1 but you are assigning 500 to the value. Can you assign any max and $$anonymous$$ values to the slider?
Thanks!
Yes. See the Start() $$anonymous$$ethod of my Script (Lines 18 & 19). Its a public variable of the Component named Slider. It's much easier because you dont have to normalize your Health to the intervall [0, 1] that way.
Answer by mazo0 · Nov 03, 2019 at 06:54 PM
This is how I could do that
public Slider slider;
...
Color color = new Color(233f/255f, 79f/255f, 55f/255f);
slider.gameObject.transform.Find("Fill Area").Find("Fill").GetComponent<Image>().color = color;
Your answer
Follow this Question
Related Questions
GUI.RepeatButton funcionality with new Unity UI (4.6.x) 2 Answers
Fall down a line in Unity 4.6 text via script 3 Answers
Particle System In 4.6 UI 6 Answers
How to convert old GUI to new 4.6 UI? 1 Answer
Push a UI-Element away with another 1 Answer