15

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.

MatthewD
  • 6,719
  • 5
  • 22
  • 41
Filip Vondrášek
  • 1,208
  • 1
  • 13
  • 26

4 Answers4

41

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

Ninja Dev
  • 816
  • 8
  • 10
  • 1
    This was the answer that helped me. – Alpants Sep 11 '16 at 17:31
  • 1
    This is actually the best answer. Thank you! – Tigertron Mar 10 '19 at 04:10
  • This option does not appear to me – Michael Pacheco Nov 27 '19 at 15:22
  • @MichaelPacheco Your method needs to take one *float* parameter in order for this option to appear on the pop up menu as in the picture. – ecv Jan 06 '21 at 02:04
  • Thank you for this. I find the alternative of passing a reference to the slider quite silly, as you could simply store that reference at design time, I mean unless you're creating sliders on the go, whose references you could keep too on the fly anyways. This makes sense. Passing a simple value like this makes more sense to me, even if I have to cast to int later on. – ecv Jan 06 '21 at 02:07
32

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 :-)

Jesper
  • 402
  • 5
  • 10
  • Thanks, that works! :) But how is it possible that it works fine with the other sliders I have in the scene? All of my event handlers are the same (for now), but this was the only one that showed the fourth box. – Filip Vondrášek Sep 20 '15 at 14:55
  • 3
    Are you sure you selected the right one below Dynamic float http://imgur.com/5325W7m – Jesper Sep 20 '15 at 15:06
  • I don't know what to say. That was the exact problem. Thank you very much, Jesper! – Filip Vondrášek Sep 20 '15 at 15:13
  • This is the first time I'm trying to do something in Unity on my own... :) – Filip Vondrášek Sep 20 '15 at 15:13
  • This works, but it's a bit quirky. If you just want the value (usually) then @dev_ter's answer is more straightforward: simply accept a float value (like in the OP) but make sure you select your function from the "Dynamic float" section (and then there will be no numeric field in the inspector). – oferei May 25 '16 at 14:13
1

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.

Mi-Creativity
  • 9,554
  • 10
  • 38
  • 47
Aaron Hull
  • 422
  • 4
  • 16
0

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.

Michael Urvan
  • 492
  • 3
  • 8