Assigning an OnValueChanged gamobject via script. (C#)

I’ve looked through several threads about this particular subject, and yet none of them have given me any good solutions.

What I want, on Start, is for a Slider to find the MainCamera in a Scene and Assign that Camera to the OnvalueChanged Function of the Slider along with its fieldofview.

attach the script below to the Slider

Slider sli;

void Start()
{
   sli = GetComponent<Slider>();

   sli.minValue = 30f;
   sli.maxValue = 90f;

   sli.onValueChanged.AddListener((float value) =>
   {
         Camera.main.fieldOfView = sli.value;
   });
}

the script above assume the camera is tagged as ‘MainCamera’

1 Like

While your script works, I now have the problem of my slider automatically disabling when transitioning between level, preventing me from moving it. Why is this the case? I have no other scripts controlling it.