Search Unity

Showing specific text when a value is the highest of a few

Discussion in '2D' started by Leemus, Apr 24, 2019.

  1. Leemus

    Leemus

    Joined:
    Oct 12, 2018
    Posts:
    6
    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"

    carnival.png
    if anyone has any suggestions I would be so grateful! I'm a bit of a beginner but I'm keen to learn!
    thank you in advance! :)
     
  2. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,807
    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:
    Code (CSharp):
    1. using UnityEngine.UI;
    2. public class Example : MonoBehaviour
    3. {
    4.     public Slider redSlider;
    5.     public Slider greenSlider;
    6.     public Slider blueSlider;
    7.     public Text bottomText;
    8.     public string bottomTextFormat = "People are most likely to pick a sweet that is {0}";
    9.  
    10.     private void OnEnable()
    11.     {
    12.         ListenForSlidersChanging();
    13.     }
    14.  
    15.     private void OnDisable()
    16.     {
    17.         StopListeningForSlidersChanging();
    18.     }
    19.  
    20.     private void ListenForSlidersChanging()
    21.     {
    22.         // these sliders will call the function OnSliderValuesChanged when the value changes
    23.         redSlider.onValueChanged.AddListener(OnSliderValuesChanged);
    24.         greenSlider.onValueChanged.AddListener(OnSliderValuesChanged);
    25.         blueSlider.onValueChanged.AddListener(OnSliderValuesChanged);
    26.     }
    27.  
    28.     private void StopListeningForSlidersChanging()
    29.     {
    30.         // OnSliderValuesChanged will no longer be called
    31.         redSlider.onValueChanged.RemoveListener(OnSliderValuesChanged);
    32.         greenSlider.onValueChanged.RemoveListener(OnSliderValuesChanged);
    33.         blueSlider.onValueChanged.RemoveListener(OnSliderValuesChanged);
    34.     }
    35.  
    36.     private void OnSliderValuesChanged(float newValue)
    37.     {
    38.         UpdateBottomText();
    39.     }
    40.  
    41.     private void UpdateBottomText()
    42.     {
    43.         // is red greater than green and blue?
    44.         if (redSlider.value > greenSlider.value && redSlider.value > blueSlider.value)
    45.         {
    46.             bottomText.text = string.Format(bottomTextFormat, "RED");
    47.             return;
    48.         }
    49.  
    50.         // is green greater than red and blue?
    51.         if (greenSlider.value > redSlider.value && greenSlider.value > blueSlider.value)
    52.         {
    53.             bottomText.text = string.Format(bottomTextFormat, "GREEN");
    54.             return;
    55.         }
    56.  
    57.         // is blue greater than red and green?
    58.         if (blueSlider.value > redSlider.value && blueSlider.value > greenSlider.value)
    59.         {
    60.             bottomText.text = string.Format(bottomTextFormat, "BLUE");
    61.             return;
    62.         }
    63.  
    64.         // otherwise what should be shown? Nothing?
    65.         bottomText.text = string.Empty;
    66.     }
    67. }
     
    Leemus likes this.
  3. Leemus

    Leemus

    Joined:
    Oct 12, 2018
    Posts:
    6

    thank you very much! this works spot on!
     
    LiterallyJeff likes this.
  4. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,807
    Glad it works for you, if you have any questions about how the code works let me know.