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

Slider OnValueChanged not working at runtime

Discussion in 'UGUI & TextMesh Pro' started by paulindrome, Aug 10, 2020.

  1. paulindrome

    paulindrome

    Joined:
    Mar 10, 2018
    Posts:
    29
    In Unity 2020.1 the OnValueChanged callback of the Slider component seems to refuse to work at runtime.

    I've basically written this bridge between a Slider and a Text showing its value multiple times for different projects already and I thought I was quite familiar with the technique. However, in Unity 2020.1 the usual approach doesn't seem to work.
    The script basically has a current and a max value (f.e. for a player's health value) and sets the TextMeshProUGUI text property to a formatted string whenever the value of the Slider changes.
    However, this approach no longer seems to function, no matter how I set the text (text = ... or SetText(...) ) or what the callback in the script looks like (usually, I just do this in the set{} blocks of public properties).

    I've uploaded a short video of the behaviour in editor and at runtime and attached it below. In the code, I have omitted any Debug.Log calls.

    Code (CSharp):
    1. public class UIResourceText : TextMeshProUGUI {
    2.     float currentValue = 1000, maxValue = 1000;
    3.     public string formatString;
    4.  
    5.     new void Awake(){
    6.         base.Awake();
    7.         formatString = text;
    8.     }
    9.  
    10.     public float CurrentValue {
    11.         get { return currentValue; }
    12.         set { currentValue = Mathf.Clamp(value, 0, maxValue); }
    13.     }
    14.  
    15.     public float MaxValue {
    16.         get { return maxValue; }
    17.         set { maxValue = Mathf.Clamp(value, 0, float.MaxValue); }
    18.     }
    19.  
    20.     public void SetCurrentValue(float value){
    21.         CurrentValue = value;
    22.         UpdateText();
    23.     }
    24.  
    25.     public void SetMaxValue(float value){
    26.         MaxValue = value;
    27.         UpdateText();
    28.     }
    29.  
    30.     public void SetText(float value){
    31.         text = string.Format("{0} / {1}", value, maxValue);
    32.     }
    33.  
    34.     void UpdateText(){
    35.         base.SetText("{0} / {1}", currentValue, maxValue);
    36.     }
    37. }
     
    akuno likes this.
  2. bagstoper

    bagstoper

    Joined:
    Oct 21, 2020
    Posts:
    1
    I am reporting the same thing did you ever find a fix?

    EDIT: No "fix" found. I updated unity to the newest version. Still had the same problem. Code worked in editor would not work at run time.

    Fast forward two days. Think this is an actually a unity bug. Try to recreate problem in empty project, same code works without any changes. Go back to original project and try it again there, now it works in editor and runtime.
     
    Last edited: Nov 30, 2020
  3. HeyBishop

    HeyBishop

    Joined:
    Jun 22, 2017
    Posts:
    238
    Fast forward seven months, and I think this is an actual Unity bug too. I'm having the same issue. It works only if the event is set to 'Editor And Runtime'.
     
    akuno likes this.
  4. Rib

    Rib

    Joined:
    Nov 7, 2013
    Posts:
    39
    Er, this is a bit bizarre, I'm seeing this in Unity 2019.4.25f1 with an in-game volume slider that has been working fine in-production via a Runtime-only OnValueChanged event callback. If I'm in play mode and simply toggle the event to 'Editor and Runtime' and then back to 'Runtime' it magically starts working as expected!? o_O
     
  5. Rib

    Rib

    Joined:
    Nov 7, 2013
    Posts:
    39
    Very odd, yeah, while in play mode I can also toggle the event to Off then back to Runtime or I can toggle whether the slider is interactive and those redundant actions seem to kick it into working - definitely seems like a Unity bug here.
     
  6. Rib

    Rib

    Joined:
    Nov 7, 2013
    Posts:
    39
    Apparently _any_change to the slider while in play mode (even just changing the 'normal' color) is enough to kick the slider in to working.

    Ah, okey - I did recently turn on the "experimental" editor optimization for being able to transition into play mode faster by disabling the 'domain reload'. Turning that off under Project Settings -> Editor -> Enter Play Mode Settings actually fixes this issue for me.

    I guess then that there's some static state in Unity's slider implementation and it maybe needs updating to support this play mode optimization.
     
    edin97, Eduardo_Morato, akuno and 2 others like this.
  7. betomaluje

    betomaluje

    Joined:
    Mar 23, 2019
    Posts:
    17
    Strange thing but as @Rib said, doing this Project Settings -> Editor -> Enter Play Mode Settings actually fixes this issue for me.
     
    arkano22 and ProGameDevUser like this.
  8. akuno

    akuno

    Joined:
    Dec 14, 2015
    Posts:
    73
    @Rib is right on point.