Set Slider value but don't trigger On Value Changed

I have another scripts attached to On Value Changed and do not want to trigger them. Is this possible?

For example I want a toggle to reset all sliders to default values.

I had a similar issue with loading saved values for settings on game load. My solution was to have a bool set that the script looks for, and if set, ignores the change. So you could possibly do something like this:


public class SettingsHandler
{
  public static bool ignoreValueChanges;
  public void ResetValues()
  {
    ignoreValueChanges = true;
    slider1.value = slider1Default;
    slider2.value = slider2Default;
    ignoreValueChanges = false;
  }
}
public class OtherClass
{
  OnValueChange(float value)
  {
    if(SettingsHandler.ignoreValueChanges)
      return;
   //Other code here
  }
}

The OnValueChange code gets called immediately when the value changes, so the bool can be set back to false after all sliders/toggles/etc are changed. With a static bool each script won’t need a reference and can just access it.

This works in Unity 2020.

toggle.SetIsOnWithoutNotify(value);
slider.SetValueWithoutNotify(value);

I think that the best thing to do is to create a boolean global flag that is checked by the listeners: if the last click was a reset, don’t do anything. As soon as you move the slider you clear that boolean flag.

What worked for me and is maybe a bit less static :

  1. delete the listener (the On Value Changed directive) in the Editor.
  2. on Initialization use this code :
    myslider.value = someScriptableObject_Or_Initialization_Value;
    myslider.onValueChanged.AddListener(
    delegate { actionToPerformOnValueChanged(); }
    );