Filip Vondrášek
Filip Vondrášek

Reputation: 1208

Unity3D 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.

enter image description here

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

Answers (4)

Ninja Dev
Ninja Dev

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

click here for the image

Upvotes: 42

Michael Urvan
Michael Urvan

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

Aaron Hull
Aaron Hull

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:

  1. 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).

  2. 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

Jesper
Jesper

Reputation: 422

You can do this

public void ChangeWindDirection(Slider slider)
{
    Debug.Log("New wind direction: " + slider.value);
}

Then assign the slider: enter image description here

That way you can get the slider value, or other things the slider has :-) Hope that's what you wanted :-)

Upvotes: 33

Related Questions