Change value of a variable in a script with a slider?


I am currently messing around with the new sliders. I want to have my slider change the value of the float called “musicVolume” in the below script when the slider’s value changes. The script is attached as a component to a game object.

using UnityEngine;
using System.Collections;

public class GameController : MonoBehaviour {

    public float musicVolume = 1.0f;
    public float sfxVolume = 1.0f;
    public int score = 0;
    //There is other stuff past here, should have no effect though.

I dragged this game object with the attached script onto the object parameter in “On Value Changed” and clicked the drop down list for functions. But I didn’t see any of my float variables show up. Do I have to change the variables through a function? I thought I could change variables directly, since variables like “bool enabled” show up. If I can change variables correctly, how can I make them show up?


Thanks in advance! :smiley:

Only methods show up. But properties are methods. So you can do this:

public float musicVolume {get; set;}

And everything will work