sorry if that title made no sense! I’m currently prototyping an educational game. basically what I am trying to do is have the word “red” on the end of the sentence at the bottom change to match the colour on the slider that is set to the highest number when the player slides the slider to a number so if the slider labelled green is on a higher number than the others the text at the bottom will read " people are most likely to pick a sweet that is GREEN"
You’ll need a script reference to that text field, and also those three sliders. Then in code you’ll need to listen for when a slider is changed, and update the text based on which of the sliders is now the greatest value.
I just whipped up this example for you, it is not the best way to do this if you need more sliders eventually, because this manually compares each slider to the others individually. But if this won’t get more complicated then this should work fine or at least get you on the right track:
using UnityEngine.UI;
public class Example : MonoBehaviour
{
public Slider redSlider;
public Slider greenSlider;
public Slider blueSlider;
public Text bottomText;
public string bottomTextFormat = "People are most likely to pick a sweet that is {0}";
private void OnEnable()
{
ListenForSlidersChanging();
}
private void OnDisable()
{
StopListeningForSlidersChanging();
}
private void ListenForSlidersChanging()
{
// these sliders will call the function OnSliderValuesChanged when the value changes
redSlider.onValueChanged.AddListener(OnSliderValuesChanged);
greenSlider.onValueChanged.AddListener(OnSliderValuesChanged);
blueSlider.onValueChanged.AddListener(OnSliderValuesChanged);
}
private void StopListeningForSlidersChanging()
{
// OnSliderValuesChanged will no longer be called
redSlider.onValueChanged.RemoveListener(OnSliderValuesChanged);
greenSlider.onValueChanged.RemoveListener(OnSliderValuesChanged);
blueSlider.onValueChanged.RemoveListener(OnSliderValuesChanged);
}
private void OnSliderValuesChanged(float newValue)
{
UpdateBottomText();
}
private void UpdateBottomText()
{
// is red greater than green and blue?
if (redSlider.value > greenSlider.value && redSlider.value > blueSlider.value)
{
bottomText.text = string.Format(bottomTextFormat, "RED");
return;
}
// is green greater than red and blue?
if (greenSlider.value > redSlider.value && greenSlider.value > blueSlider.value)
{
bottomText.text = string.Format(bottomTextFormat, "GREEN");
return;
}
// is blue greater than red and green?
if (blueSlider.value > redSlider.value && blueSlider.value > greenSlider.value)
{
bottomText.text = string.Format(bottomTextFormat, "BLUE");
return;
}
// otherwise what should be shown? Nothing?
bottomText.text = string.Empty;
}
}