- Home /
(4.6 UI) Use a slider's "Value" as parameter to attached funct?
If I add a function from a script to the OnValueChanged part of a slider, and the function takes a float parameter, can I use the Value (from 0 to 1) parameter of the slider in that function? In other words, 1) is there a way to access that property in the code (meaning I don't need a parameter), or 2) is there a way to sort of drag and drop the Value property to the parameter slot so the parameter reads the value rather than a number I type in?
If your function is public and only takes 1 float value as its only parameter, then it should show up in the drop down list.
public void Test(float a){
Debug.Log("It works and the value is: " + a);
}
Answer by Packman0612 · Sep 07, 2014 at 11:05 PM
Try this: (Javascript)
var volume : float = 0.3f; // set this to whatever default volume you want
public function VolumeSlider (value : float){
volume = AudioListener.volume = value; // this changes the volume
}
and then when assigning the function to the slider from the dropdown, instead of using "VolumeSlider(float)", use the one called "VolumeSlider" (may be towards the top of the list).
ins$$anonymous$$d of using "VolumeSlider(float)", use the one called "VolumeSlider"
Thank you for pointing out the different forms of the function! I hadn't spotted the "Dynamic float" category at the top of the list and was getting very frustrated by the inability to pass the slider value.