Sliders OnValueChange

Hi there,

I need to call a function whenever my slider’s value is changed, but changed only when I stop pressing my mouse. So the OnValueChange should not work until I stop dragging. Is there a simple way to do this??

Greetings @Furret_

I wouldn’t say this way is simple - it involves using the Unity Event System and adding the IPointerUpHandler interface to your Class definition. Having said that, if you attach this code to your slider, it should work OK. Make sure you have added the Unity UI Package to your project. The new value on mouse up is accessible where I have the print statement. Grab your value there…

using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;

public class SliderScript : MonoBehaviour, IPointerUpHandler
{
    Slider slider;
    float oldSliderValue;

    void Awake()
    {
        slider = GetComponent<Slider>();
    }

    void Start()
    {
        oldSliderValue = slider.value;
    }

    public void OnPointerUp(PointerEventData pointerEventData)
    {
        if (slider.value != oldSliderValue)
        {
            print("Slider value is now " + slider.value);
            oldSliderValue = slider.value;
        }
    }
}