- Home /
adjusting volume for audio listener
I have a problem in adjusting the volume of my Audio Listener. When I tried to drag the thumb of the horizontal slider that I had created, the volume of the background music is still remain the same which was set as 10. Is there anything that I should modify in my script? Here is the script:
var hSliderValue : float = 2.0; static var volume : float; function OnGUI () {
hSliderValue = GUI.HorizontalSlider (Rect (50, 25, 100, 30), hSliderValue, 0.0, 10.0); GUI.Box(Rect(0,0,200,50),"Volume"); AudioListener.volume = 5 * hSliderValue; }
Answer by Joshua · Mar 02, 2011 at 01:48 PM
Like Jessy said, only values between zero and one work. So replace the last line of code you have by:
AudioListener.volume = hSliderValue/10; } and everything should work.
Sounds like a lot of useless work for the CPU. I recommend AudioListener.volume = GUI.HorizontalSlider (Rect (50, 25, 100, 30), AudioListener.volume , 0, 1);
thanks so much, it does work well.... by the way, I would like to create one option button in my main menu which allow players to control the volume of the background music in another scene? how could I link/connect the options control to another scene? thanks in advance....
Answer by Jessy · Mar 02, 2011 at 01:35 PM
Only values between zero and one do anything. You can't boost volume levels.
Answer by jchart7 · Feb 10, 2015 at 06:22 PM
I just created another Audio Source and called it MasterVolume, then i linked its values to the Audio Listener. Then use your slider to control the MasterVolume.
using UnityEngine; using System.Collections; public class AudioControls : MonoBehaviour { public GameObject masterVolume; private float aSliderValue = 1; void Update () { aSliderValue = masterVolume.audio.volume; AudioListener.volume = aSliderValue; } }
Your answer
![](https://koobas.hobune.stream/wayback/20220613110953im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Inconsistent ReverbZone / AudioSource.volume behavior 2 Answers
Breathing volume increases as player gets more tired 0 Answers
Android: Ringer Volume Control 0 Answers
what audio.volume level on iphone would match the music player volume? 1 Answer
Fading out before load script not functioning correctly? 2 Answers