- Home /
How change slider(prefab) fill color in a script?
Hi guys,
I have a healthbar attached to my prefab enemy tanks in a "desert strike" clone I'm building.
I have created a healthbar slider in a worldspace canvas and attached this canvas and script to my enemy prefab - It all works great so far.
Problem: I want to start the fill color at green and reduce it to red as the health value decreases. I know how to lerp the color, but I just cant work out how to actually change the color of the object! I have scoured the internet for an answer to this but its not even in the unity scripting manual!
I understand the fil color is at: Slider>Fill Area>Fill>Image>color. But how do I change this in c#???
Cheers
Answer by orchard800 · Oct 18, 2015 at 08:38 PM
Ok, this has now been addressed in the Unity Tanks! tutorial. Details added for others' benefit.
private void SetHealthUI ()
{
// Set the slider's value appropriately.
m_Slider.value = m_CurrentHealth;
// Interpolate the color of the bar between the chosen colours based on the current percentage of the starting health.
m_FillImage.color = Color.Lerp (m_ZeroHealthColor, m_FullHealthColor, m_CurrentHealth / m_StartingHealth);
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612034328im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
Change slider color to green when value is 50 2 Answers
Multiple Cars not working 1 Answer
Slider to modify a countdown time in another scene 0 Answers
How can I change a tile color in Unity by using C#., 2 Answers