Volume Slider - Need to know how to link it to Audio.Source

Hello everyone,

I'm making a racing game and on the main menu I want to have a volume slider so the player can adjust the volume from simply sliding it from right to left.

I currently have the Slider created and in position, I just need to know how to link it to the Audio.source?

Here's what I have so far:

var hSliderValue : float = 0.0;

function OnGUI () {
    hSliderValue = GUI.HorizontalSlider (Rect (370, 220, 546, 30), hSliderValue, 0.0, 10.0);
}

Any help would be appreciated!

function Update(){
   AudioListener.volume = hSliderValue/10.0;
}

(10.0 is your slider bar max value.)

Can't you simply adjust the volume-property of the audio source?

From the docs:

public class example : MonoBehaviour {
    void Awake() {
        audio.volume = 0.2F;
    }
}

http://unity3d.com/support/documentation/ScriptReference/AudioSource-volume.html

var hSliderValue : float = 0.0;

function OnGUI () {
    hSliderValue = GUI.HorizontalSlider (Rect (370, 220, 546, 30), hSliderValue, 0.0, 10.0);
    audio.volume = hSliderValue;
}

Couldn’t you just link the two values using the slider’s “On Value Changed” event?

Add a new event to the slider’s “On Value Changed” section, drag your audio source to it, then select AudioSource.volume (the one in dynamic float, not static parameters). That should link the two values without requiring an extra script.

I found a YouTube video that used this method: - YouTube

Volume / Slinder.Value * Silinder.MaxValue