- Home /
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!
Answer by Dreamer · May 11, 2011 at 08:54 AM
function Update(){
AudioListener.volume = hSliderValue/10.0;
}
(10.0 is your slider bar max value.)
Or he could simply set the range of the HorizontalSlider to 0-1.0 ins$$anonymous$$d of 0-10.0, and then dividing by 10 again when using the value...
This should make it easier to use: http://www.unifycommunity.com/wiki/index.php?title=Loudness
Answer by CHPedersen · May 11, 2011 at 08:28 AM
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
That would set the volume for a single audio source. If you set the volume for the audio listener, then it's an overall volume control, which seems to be what the question is asking for. (Then you can use AudioSource.ignoreListenerVolume for the music, which you'd likely want to be controlled separately.)
Answer by androids · Sep 23, 2013 at 01:27 PM
var hSliderValue : float = 0.0;
function OnGUI () {
hSliderValue = GUI.HorizontalSlider (Rect (370, 220, 546, 30), hSliderValue, 0.0, 10.0);
audio.volume = hSliderValue;
}
Answer by Penra · Sep 20, 2015 at 10:35 PM
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: https://www.youtube.com/watch?v=gK-8n3IZlJo
Your answer
Follow this Question
Related Questions
Multiple, independent volume sliders 1 Answer
Music play throughout all scenes? 1 Answer
Controlling Master Volume with one slider? 1 Answer
how to change and save audio volume from a different scene with a slider 1 Answer
The volume does not reduce when master volume is lowered using volume slider 3 Answers