Hooking Up To A Slider

I’m trying to make slider “Throttle” set a value. There is no gameObject value, rather I’m trying to change a value in a script. I’ve attached my ThrottleControl script to the “On Value Changed” component in the inspector. The script includes a public function with a float parameter.

I’ve watched the “Slider” tutorial and it says that this should expose that function so that I can select it in the inspector. It doesn’t. Any ideas what newbie mistake I’m making here?

using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class ThrottleControl : MonoBehaviour
{
   public Slider sliderVar;
   public float ThrottlePosition;


   public void SliderTest (ref float passedValue)
   {
     Debug.Log ("Throttle onValueChanged, Slider = " + passedValue);
     //return(0f);
   }

Is the ref property necessary? Try without it. When you click the dropdown to select the function to call, make sure you select the callback under the “Dynamic Float” option so that it will pass the value that the slider currently is.

Also, if you are just interested in updating a value, you can do it as a public property of your script instead:

public float mySliderFloat { get; set; }

And you could even bind to the set itself to do something if it gets changed…

private float _myFloat
public float MyFloat
{
    get { return _myFloat; }
    set
    {
        _myFloat = value;
        // do something with value here
    }
}

Thanks for looking at this.

The ref was a shot in the dark. I’ve included a screenshot after writing a very simple script that makes two public functions available that I would have thought to meed the criteria. As you can see, I’ve dragged “TestMyFloat” into the “On Value Changed” property box, clicked on the function, but nothing shows up.

Have my newbie eyes missed something painfully obvious?

Jeff G.

    public class TestMyFloat : MonoBehaviour
    {
       public float MySliderFloat2(float passedThing)
       {
         return 0f;
         Debug.Log ("MySliderFloat2 ran");
       
       }
       public float MySliderFloat()
       {
         return 0f;
         Debug.Log ("MySliderFloat ran");
       }
    }

}

the method signature you use is wrong. You cant return a value.

public void OnValueChanged( float value )
{
    Debug.Log( value );
}
1 Like

OK, I tried that, still no luck. Here is a script that I have attached to my slider (which I don’t think is required). Sill I don’t have any valid choices in the function dropdown.

Any help will be appreciated!

My test code:

using UnityEngine;
using System.Collections;

public class SliderFloatTest : MonoBehaviour {
       

    public void OnValueChanged(float value)
    {
        Debug.Log ("OnValueChanged, value=" + value);
    }
   
    public float OnValueChanged2()
    {
        Debug.Log ("float OnValueChanged2 ran");
        return 1.1f;
    }
   

    // Use this for initialization
    void Start ()
    {
        Debug.Log ("TestMyFloat started");
    }
   
}
 public class ThrottlePositionScript : MonoBehaviour
{

public float ThrottlePosition ;

.                public void AdjustThrottle (float NewestThrottle)
.             {
.             ThrottlePosition = NewestThrottle;
.              }
}

Then in the editor, on the relevant slider click the + symbol, drag your 'ThrottlePositionScript in, and select the AdjustThrottle function… It’s possible I have missed your point, but this is how I have my slider set up and it works ‘spiffingly’ :smile:

Thanks for taking the time.

I’ve uploaded a screenshot with arrows to show exactly what I did and what I saw when I tried to see the function. It’s still not working – I don’t see the “AdjustThrottle” in the drop down list. Puzzling indeed – I’ve gotten everything else working, and even a slider using a completely different method, but not what should be the easiest method. No doubt I’m making some weird little error somewhere.

If you look at the uploaded screenshot, here’s what the numbers correspond to:

  1. File | Creat new project.
  2. GameObject | UI | Slider – drops a slider in the scene.
  3. Project | Create new script, call it ThrottlePositionScript.
  4. Write the script, copied from your post. F8 to make sure it compiles, save.
  5. Select the Slider then add an “On Value Changed” space (at 7)
  6. Drag the slider script into the “On Value Changed” space.
  7. Click on the function dropdown.

As you can see, no functions are available.

Appreciate the help,

Jeff G.

1948949--126011--UnityScreenShot-Slider-WithArrows.jpg

ahh no. you need to apply the ThrottlePositionScript to a gameobject. Any will do. Than drag the gameobject where the script is attached to, to the sliders OnValueChaged callback

2 Likes

What I have learned to do… (As element_wsc suggests)… If you have random scripts such as these, attach them to an empty gameObject, you could call the object ‘ThrottleScriptObject’ or whatever… (You might want to parent these random scripts onto another empty gameObject, say called… ‘ScriptPlacement’ (To keep the heirachy clean and tidy) Then You drag this new gameObject onto the ‘+’ part of the slider… This will allow you to find the script reference you have created… It seems its all about ‘objects’… Scripts need to be on an object… :smile: Good luck, let us know if you have cracked it, or if you need further help :smile:

Dang you rock. element_wsc I only wish I could buy you beer/wine/helicopter ride, etc.

That’s all it took.

Genkidevelopment, I’ll take your advice from now on and attach scripts to objects. I had kind of arrived at that conclusion when creating my own classes but figured it was a kluge. Maybe not. And hey, it works.

Thank you!!!

Hopefully someday I’ll be able to return the favor to another newbie.