Slider onValueChanged sending only 0 (or other defined value)
I'm trying to attach an onValueChanged event to my GUI Slider, but for some reason it gives me a mandatory option to put a value in (it's the fourth box in the "On Value Change (Single)" section). Then it only sends the forementioned value, not the actual value of the slider.
The code for the event is as follows:
public void ChangeWindDirection(float value)
{
Debug.Log("New wind direction: " + value);
}
I tried restarting both Unity and Visual Studio 2013 to no avail. Now it even puts the box with a value to every new event I try to create.
Answer by Berzeger · Sep 20, 2015 at 02:57 PM
Someone at Stackoverflow found a solution:
You can do this
public void ChangeWindDirection(Slider slider)
{
Debug.Log("New wind direction: " + slider.value);
}
Then assign the slider.
That way you can get the slider value, or other things the slider has :-) Hope that's what you wanted :-)
Answer by EyePD · May 19, 2016 at 07:28 PM
I was having a similar problem. I found the Stackoverflow and realized that my function that receives the float was listed twice and I was selecting the wrong one; I had to select the one under "Dynamic". See this image from the stack: http://imgur.com/5325W7m
I just had the same issue. I realised that even when you set the Slider to use whole numbers, it will only map to float values. $$anonymous$$apping to an int will result in this extra value field and will never pass the value. I just created a function that takes a float and cast it to an int. It would be nice to have IntSliders..
Phew, finally! This looks like the solution Unity intended. I solved it some time ago, then forgot about it. Bookmarking.