- Home /
Using a Slider to change a color to pre-defined colors based on slider value
I'm using the 4.6 beta incase that makes a difference.
I have a slider with a range of 0-100 and I want to have the color of on Image of a child to be changed to pre-defined colors based on what the value of the slider is.
I want the range for each color to be:
Colors[0] 0-14
Colors[1] 15-34
Colors[2] 35-63
Colors[3] 64-100
When the value of the slider reaches the next set of ranged numbers I want the color to change to the color for that range.
When I hit play to see if it works the first color, Colors[0], shows as its suppose to but changing the slider value doesn't change the color.
I think the problem is either with if statements or with the public void code. When I first found out about that code it was used in an example to use the slider to change volume and the code initially read as:
public void GoodSlider(float value)
{
Goodvalue = AudioListener.volume = value;
}
Since I didn't want to change the audio volume I just removed that part.
I'm at a lost to why the if statements aren't working to show the other colors and need some help and/or direction.
Here's my full script:
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class TestMenu : MonoBehaviour
{
Color32[] Colors;
float Goodvalue = 3f;
Image[] images;
public void GoodSlider(float value)
{
Goodvalue = value;
}
void Awake()
{
Colors = new Color32[4];
Colors[0] = new Color32 (185, 0, 185, 255);
Colors[1] = new Color32(0,127,255,255);
Colors[2] = new Color32(0,0,255,255);
Colors[3] = new Color32(0,255,255,255);
}
void Update()
{
images = gameObject.GetComponentsInChildren<Image>();
foreach (Image image in images)
{
if(Goodvalue >= 0f && Goodvalue <= 14f)
{
image.color = Colors[0];
}
if(Goodvalue >= 15f && Goodvalue <= 34f)
{
image.color = Colors[1];
}
if(Goodvalue >= 35f && Goodvalue <= 63f)
{
image.color = Colors[2];
}
if(Goodvalue >= 64f && Goodvalue <= 100f)
{
image.color = Colors[3];
}
}
}
}
I find nothing wrong with your 'if' statements that would cause the problem you specify. For efficiency, you should do the 'if' once and calculate a color, then apply that color to each to each 'Image', and if you really want to be efficient, only do the change when 'Goodvalue' changes, not in Update(). Bug that's efficiency.
It is likely that the issue with the color not changing is with your Image class, not with this code.
I couldn't find any errors here either. I would suggest doing Debug.Log all over the show to find out where your code is stopping.
@robertbu The Image class is a unity class added in 4.6 It does have a public color variable. I'm not sure the problem is there.
@Bored$$anonymous$$ormon - Thanks for the heads up on the Image class. I'm still using EZGUI for my interface work, so I have not spend much time with uGUI.
Answer by MarksTeq · Oct 22, 2014 at 09:45 PM
You never actually called the function: public void GoodSlider(float value)
So just add the line to update and it should work:
void Update()
{
GoodSlider(this.GetComponent<Slider>().value); //Add this
images = gameObject.GetComponentsInChildren<Image>();
foreach (Image image in images)
{
if (Goodvalue >= 0f && Goodvalue <= 14f)
{
image.color = Colors[0];
}
if (Goodvalue >= 15f && Goodvalue <= 34f)
{
image.color = Colors[1];
}
if (Goodvalue >= 35f && Goodvalue <= 63f)
{
image.color = Colors[2];
}
if (Goodvalue >= 64f && Goodvalue <= 100f)
{
image.color = Colors[3];
}
}
}
Side note (untested) : Why don't you just make your slider go from 0 to 3 for each of the colours? (You can even tick [Whole Numbers] to snap to those numbers) because then you can just:
image.color = Colors[(int)this.GetComponent<Slider>().value];
or
image.color = Colors[(int)Goodvalue];
Instead of the ifs...
An even more efficient way would be to create a ChangeColor method and call it on the sliders OnValueChanged method:
public void changeColor()
{
GoodSlider(this.GetComponent<Slider>().value);
images = gameObject.GetComponentsInChildren<Image>();
foreach (Image image in images)
{
image.color = Colors[(int)Goodvalue];
}
}
GoodSlider should be called from the OnChange event of the Slider. Not much point calling it every frame unless the value changes every frame.
@Bored$$anonymous$$ormon, I was actually putting that together as you posted that xD
But the original reply was to show the OP what he/she did wrong so that he/she could learn and once he/she understands the situation, then he/she can continue to code optimisation...
Sorry for the late response I went out to eat.
That line of code definitely worked and did the trick only issue I realized was I put 2 of the colors in the wrong spot so I just need to change that which is easy.
The only reason I don't do the 0-3 range as you suggested is because I have the numbers for each color based on an image and each box color I have on the image is different size.
For the OnChange event I wasn't exactly sure how it worked, I tried to figure out how to implement it but decided to try to just stick with the basic part first then maybe get to that after. I'll try to do that now to see how it goes. This gave me a huge headache last night while trying to figure it out. Thanks a bunch to everyone.
All you do is create a public function that returns void and has either no or one parameter of (string/int/bool/GameObject) I created the public void changeColor() (see Above) Then you can just put all your code into that method and you drag the slider object into the object box of the OnValueChanged and you then browse for your method :)
Your answer
![](https://koobas.hobune.stream/wayback/20220613165715im_/https://answers.unity.com/themes/thub/images/avi.jpg)