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
  4. Dismiss Notice

Question Slider graphics not updating, value is updating.

Discussion in 'Scripting' started by k1tsunee, Jan 5, 2021.

  1. k1tsunee

    k1tsunee

    Joined:
    Jan 5, 2021
    Posts:
    4
    Hey guys!
    I'm trying to do a slider countdown (5 seconds). I found this thread but I can't seem to make the algorithm work.
    The variable
    normalizedTime
    is updating as well as
    coundtownBarSlider.value
    . However, the graphics for the slider are not updating, the bar always starts as a random value and stays that way.

    Code (CSharp):
    1. IEnumerator countdown(){
    2.         float duration = 5f;
    3.         float normalizedTime = 0f;
    4.         GameObject countdownBarGO = calculusUIPrefab.transform.GetChild(5).gameObject;
    5.         Slider countdownBarSlider = countdownBarGO.GetComponent<Slider>();
    6.         countdownBarSlider.value = 0f;
    7.      
    8.         while(normalizedTime <= 1f){
    9.             countdownBarSlider.value = normalizedTime;
    10.             Debug.Log("Slider: " + countdownBarSlider.value.ToString());
    11.             normalizedTime += Time.deltaTime / duration;
    12.             Debug.Log("Value: " + normalizedTime.ToString());
    13.             yield return null;
    14.         }
    15.     }

    Proof that the value of the slider is, in fact, updating.
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,780
    Might not be the problem, but definitely NEVER do things like line 4 above, going for the 6th child by number! That could change so easily and you would have no idea why stuff stops working.

    Instead, when in Unity, act like Unity, and make a public field, drag the slider in:

    Code (csharp):
    1. public Slider TheSlider;
     
  3. k1tsunee

    k1tsunee

    Joined:
    Jan 5, 2021
    Posts:
    4
    The Slider is a prefab, I spawn it only in certain conditions.
    What should I do in this case?
     
  4. seejayjames

    seejayjames

    Joined:
    Jan 28, 2013
    Posts:
    685
    If you're spawning it, then no worries, just hold a reference to it when instantiating.
    For the slider visuals, not sure why it's not working...the range is set and Whole Numbers isn't checked?
     
  5. k1tsunee

    k1tsunee

    Joined:
    Jan 5, 2021
    Posts:
    4
    Tried to set the range, set min and max, nothing worked out :/
     
  6. seejayjames

    seejayjames

    Joined:
    Jan 28, 2013
    Posts:
    685
    Can you move the slider manually when you play the game and confirm the values are changing in the Inspector? Comment out any script-based slider value changes.
     
  7. k1tsunee

    k1tsunee

    Joined:
    Jan 5, 2021
    Posts:
    4
    Yep, I can.
    However, the slider value is updated once the execution of the game is paused and then unpaused.
    Otherwise, nothing happens.
     
  8. seejayjames

    seejayjames

    Joined:
    Jan 28, 2013
    Posts:
    685
    It's possible that graphics aren't updated in a while loop, I seem to remember something along those lines.... Try another approach and see.