how to link variable as inspector field

i want to implement a slider UI component which changes the value of another script on interaction.


p.e. i have a component Component with public float a.

and i have a sliderUI component SliderComponent which changes this variable based on the Input.


so onEnable the SliderComponent goes

sliderValue = Component.a

and on Change / Input it goes

Component.a = sliderValue;

but i want to reuse this component for multiple variables / sliders. meaning i want Component.a to be dynamic and assigned in the Editor / inspector.

how can i make this target an inspector property?


Overall i want similar inspector behavior to how functions are selected for UnityEvents.

But i dont know any way to pass a dynamic value into those functions / use the return value

If I understand you right, you want a two ways binding between your slider and the variable of your class?

Here is how I would do it:

[System.Serializable]
public class FloatEvent : UnityEngine.Events.UnityEvent<float> {}

private float a ;

// Attach the setter to the `onValueChanged` event of the slider
public float A
{
    get { return a ; }
    set
    {
        if( !Mathf.Approximately( a, value ) )
        {
            a = value ;
            onAValueChanged.Invoke( a ) ;
        }
    }
}

// Attach the `value` setter of the slider to the `onAValueChanged` event of the script
[SerializeField]
private FloatEvent onAValueChanged;