Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

How to change the colour of a slider based on it's current value

Discussion in 'Scripting' started by Hotpots, Oct 26, 2015.

  1. Hotpots

    Hotpots

    Joined:
    Apr 9, 2015
    Posts:
    143
    Hey guys,
    So basically what I would like to do is...:
    Keep in mind the slider is a sort of score progress bar that can be incremented and decremented.

    Min value of slider: 0
    Max value of slider: 20

    Colour of the fill of the slider when value is 0: Red
    Colour of the fill of the slider when value is 20: Green

    At the moment I have this code:
    Code (CSharp):
    1.         if (currentscore => 10)
    2.         {
    3.             sliderimage.color.y;
    4.         }
    5. if (currentscore => 15)
    6.         {
    7.             sliderimage.color.g;
    8.         }
    9.  
    As you can tell this is not doing exactly what I would want it to do :p

    However looking at Unity's scripting API I have found that Color.Lerp almost does what I want however:
    Code (CSharp):
    1. lerpedColor = Color.Lerp(Color.green, Color.red, Time.time);
    Is something I'd like to do, but not based on a time frame, based on the current score value :D

    Thanks guys!
     
    sivaavis611 likes this.
  2. Elmdran

    Elmdran

    Joined:
    Oct 28, 2014
    Posts:
    34
    Just a shot in the dark, try this:
    Code (CSharp):
    1. sliderimage.color = Color.Lerp(Color.red, Color.green, slider.value / 20);
    And use this as a function event for the slider (on slider change something something)
     
  3. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
  4. stoilcho

    stoilcho

    Joined:
    Sep 8, 2014
    Posts:
    30
    Here's my code for a slider that smoothly transitions from green to red as it shrinks:
    Code (CSharp):
    1.     void Start()
    2.     {
    3.         manager = GameObject.Find(Names.Game).GetComponentInChildren<LevelManager>();
    4.  
    5.         background = GetComponent<Image>();
    6.         slider = GetComponent<RectTransform>();
    7.         sliderMax = slider.rect.width;
    8.         sliderStep = sliderMax / manager.totalTime;
    9.  
    10.         g = new Gradient();
    11.         var gck = new GradientColorKey[2];
    12.         gck[0].color = Color.red;
    13.         gck[0].time = 0.0f;
    14.  
    15.         gck[1].color = Color.green;
    16.         gck[1].time = 1.0f;
    17.  
    18.         // Populate the alpha  keys at relative time 0 and 1  (0 and 100%)
    19.         var gak = new GradientAlphaKey[0];
    20.  
    21.         g.SetKeys(gck, gak);
    22.     }
    23.  
    24.     // Update is called once per frame
    25.     void Update()
    26.     {
    27.         background.color = g.Evaluate(manager.currentTime / manager.totalTime);
    28.         slider.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, manager.currentTime * sliderStep);
    29.     }
    The following Unity API articles show the approach. You basically give it two or more colors and you use Evaluate() with a parameter between 0 - 1 to define a mixture of these colors.
     
    Karabin and JayJack like this.
  5. JugglingJoe

    JugglingJoe

    Joined:
    Feb 15, 2020
    Posts:
    4
    Based on Elmdran's post I used:
    Code (CSharp):
    1.     public Slider slider1; //connected the slider
    2.     public Image slider1Fill; //connected the Image Fill from the slider
    3.  
    4.     slider1Fill.color = Color.Lerp(Color.red, Color.green, slider1.value / 100);
     
    sivaavis611 and blutcat like this.