Slider AddListner

I have Googled this and tried it 3 ways and nothing seems to be working. I am trying to set the “onValueChanged” in throtleSlider object in inspector during runtime. Here is the code I have been playing with:

public Slider throtleSlider;

 void Awake()
    {
        throtleSlider = GameObject.Find("Throtle - Slider").GetComponent<Slider>(); //works
        throtleSlider.onValueChanged.AddListener(CarSpeed); //1st try
        throtleSlider.onValueChanged.AddListener (delegate { CarSpeed(minspeed); }); //2nd try
        throtleSlider.onValueChanged.AddListener((newSpeed) => { CarSpeed(newSpeed); }); //3rd try
    }

public void CarSpeed(float newSpeed)
    {
    }

Now nothing happens in the inspector. Can someone show me what I am doing wrong? CarSpeed(float newSpeed) method is inside the same script as the Awake() method. If that helps.

James

In which way are you trying to set this up? What you’re saying in your first post is: subscribe the CarSpeed method to the OnValueChange event of throttleSlider. Whenever you move the slider, its event is being called by Unity, which will then update the newSpeed parameter at the CarSpeed method. Should work like that and at least of the third lambda expression, I’m sure, it should work.

So again, just to make sure you’re not misunderstanding the concept of the event: OnValueChanged is called by the slider, when you move it with the mouse. You don’t set it yourself. To set the value just to slider.value = 5f; etc.