Reputation: 1208
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.
Upvotes: 15
Views: 31994
Reputation: 826
this is the best answer i found:
(posted as a comment by Jesper)
Make sure you selected Dynamic float from the OnValueChanged drop down list in the inspector
Upvotes: 42
Reputation: 547
I had this exact same problem, old sliders were bound just fine but new sliders showed the placeholder where you put a value. Even previous sliders, if I touch them and change it to the exact same method they were now showing the value placeholder and always sending zero. I had added a slider just an hour ago just fine, but now any change to the slider OnChanged will show the placeholder value. I did add a static field to the target class in that time, not sure if that is the cause.
Upvotes: 0
Reputation: 442
If you want to pass a "dynamic variable" from Unity's UI system (i.e. ScrollRect
's scrollPosition
variable or a Slider
's value), simply create a method or property in the target script that matches the value type.
For example, ScrollRect
can send a Vector2
"dynamically" to a target script, so on the target script, you want to write a method that looks like this:
public [any return type] MyMethod(Vector2 myDynamicVar) {}
Or a property that looks like this:
public Vector2 myDynamicVar {set; [optional get;]}
This has a few vague but important implications:
the method and property written above won't appear as "dynamic" in other UI component's event callback lists if that UI component's "dynamic variable type" doesn't match (in this case Vector2
for ScrollRect
).
The method's signature needs to be JUST the dynamic variable type. i.e.: public void MyMethod(Vector2 myDynamicVar, bool isCantBeHere) {}
won't appear as "dynamic" in the component's event callback list.
I hope I didn't leave anything out, this stuff isn't well documented (or easy to find). I hope it helps.
Upvotes: 1
Reputation: 422
You can do this
public void ChangeWindDirection(Slider slider)
{
Debug.Log("New wind direction: " + slider.value);
}
That way you can get the slider value, or other things the slider has :-) Hope that's what you wanted :-)
Upvotes: 33