Tutorial: Pass Parameter with Slider’s OnValueChanged() in Unity

Overview

This article would show how to pass parameters with UI events. Here we would make a UI text listening to a UI slider’s OnValueChanged() event to display its current value.

Version

  • Unity 2017.1.1p3 (64-bit)

Step by Step

1. Create Callback Function in UI Text

We need to create a callback function for slider’s OnValueChanged(). Note that it must have a float parameter. Then add this script to your UI text.

[RequireComponent(typeof(Text))]
public class OnValueChangedText : MonoBehaviour
{
    private Text ValueText;

    private void Start()
    {
        ValueText = GetComponent<Text>();
    }

    public void OnSliderValueChanged(float value)
    {
        ValueText.text = value.ToString("0.00");
    }
}

2. Add Callback Function to Slider’s OnValueChanged()

  • Drag the UI Text object into the slider’s OnValueChanged() panel in the inspector.

  • Select OnSliderValueChanged() function below Dynamic float label. Only functions matched the event’s parameters would show below this label.

Result

The UI Text would receive every OnValueChanged() event with a float parameter now.

Leave a Comment