How can I use a slider's UnityEvent "On Value Changed" to send the slider's current value to a script?
I can not get the On Value Changed function on a slider to pass its changed value to a function. It will only ever send the value that is displayed in the input field. The manual says that it is supposed to send its changed value as an argument to the function but it is not doing that for me.
Currently, the slider has a min value of 0, max value of 10, and a default value of 0.
Screenshot of the settings
The function that is being called is just this:
public void MainVolumeControl(float vol){
Debug.Log ( "vol is: " + vol );
}
The manual states that the On Value Changed "... event can send the current value as a float type dynamic argument. The value is passed as a float type regardless of whether the Whole Numbers property is enabled."
That is just not happening. It will only ever pass what is in the last input field on the Event section - 0 in the screen-shot. If I put "3" than 3 will be the only thing passed to the function no matter what the sliders value is.
So, what am I doing wrong or is the manual wrong?
Answer by DarkGate · Mar 10, 2019 at 04:11 AM
![alt text][1]From the thread above, credit goes to Jesper.
Make sure you selected Dynamic float from the OnValueChanged drop down list in the inspector
See image: https://imgur.com/5325W7m
Answer by benio33 · Jan 09, 2020 at 05:35 AM
It is an old thread, but maybe someone will find this clarification useful, since I feel like noone made it clear enough at least ... I had to scratch my head for a bit how to bring in the "dynamic float" variables, this is how you do it.
Instead having your function declared like this:
public void MainVolumeControl(float vol){
Debug.Log ( "vol is: " + vol );
}
You should declare it like this:
public void MainVolumeControl(System.Single vol){
Debug.Log ( "vol is: " + vol );
}
That way you your "MainVolumeControl" method will show up in the menu as "dynamic float" and when you chose it from the dropdown menu the slider values will be passed automatically to the function upon value changed
Hope this helps.
Cheers, M.
Thanks so much for the clarification. Also note that your method will show up in both places, in the Dynamic Float section and the Static Parameter. $$anonymous$$ake sure you pick the Dynamics Float as mentioned previously.
This is so interesting and so important! But where TF is this documented? Is there any page on the documentation that talks about this difference and how to implement callbacks with dynamic parameters as opposed to static ones?
Answer by Mr-Men · Jul 13, 2017 at 10:37 AM
@annunciation, this worked for me - https://stackoverflow.com/a/32680604
Had to use "using UnityEngine.UI" to my script to be able to use the Slider type, but this worked for me.
Your answer
