- Home /
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.
Answer by OncaLupe · Oct 28, 2015 at 05:42 PM
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.
Are you sure that this works? I think that the ResetValues() is played atomically and the OnValueChange() is called either before, or after the ResetValue(), making the ignoreValueChanges variable useless. * Just for the pleasure of talking, I didn't try your code.
@beppim I use basically the same thing when loading saved preferences in my game and setting sliders and toggles. Each OnValueChange() gets called immediately on setting the value for me.
If for some reason it doesn't work for someone, or they just want to be extra sure, you could do this ins$$anonymous$$d:
public void ResetValues()
{
...
slider2.value = slider2Default;
Invoke("ClearIgnoreBool", 0.1f);//Calls the method after 0.1 seconds
}
void ClearIgnoreBool()
{
ignoreValueChanges = false;
}
It works because the on change event is triggered directly like a interrupt. Before ignoreValueChanges is reset to false. $$anonymous$$aybe not 100% safe, but it works for now.
Answer by beppim · Oct 28, 2015 at 05:55 PM
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.
Answer by Yiming075 · Mar 03 at 12:13 PM
This works in Unity 2020.
toggle.SetIsOnWithoutNotify(value);
slider.SetValueWithoutNotify(value);
Answer by Lumpazy · Sep 15, 2018 at 10:12 AM
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(); } );
Your answer

Follow this Question
Related Questions
how to control SMOOTHNESS via GUI slider?! It's not working...pls help! 1 Answer
How to draw slider thumb on top 0 Answers
Multiple Cars not working 1 Answer
OnGUI sliders only accept float? C# 2 Answers
Display actual number of bullets 1 Answer