- Home /
Problem implementing volume slider
Hello I'm having some difficulty with two of my volume scripts.
This is my first script, which I've named SFX Volume and attached to the Player.
var hSliderValue : float = 25.0;
function OnGUI () {
hSliderValue = GUI.HorizontalSlider (Rect (577.5, 220, 120, 30), hSliderValue, 0.0, 50.0);
}
function Update (){
AudioListener.volume = hSliderValue/50.0;
}
This is meant to control the volume level of various in-game sound effects. These sound effects are activated when the player collects an item pick up, which is dictated through this script attached to the pick ups:
var collectSound:AudioClip;
function OnTriggerEnter (other:Collider) {
if(other.gameObject.tag=="Player"){
AudioSource.PlayClipAtPoint(collectSound, transform.position);
}
}
And this is my second, separate, script which I use to control the volume for the in-game music. I named it Music Volume. Both this script and the music, as an audio clip through an Audio Source component, is attached to the Player.
var hSliderValue : float = 25.0;
var previousValue:float= hSliderValue;
var script1:MusicVolume;
function Start(){
script1 = GetComponent("MusicVolume");
}
function OnGUI () {
hSliderValue = GUI.HorizontalSlider (Rect (577.5, 280, 120, 30), hSliderValue, 0.0, 50.0);
if(previousValue != hSliderValue)Volume();
previousValue = hSliderValue;
}
function Volume(){
var audioSrc:AudioSource[];
audioSrc= FindObjectsOfType(AudioSource) as AudioSource[];
for (var aud : AudioSource in audioSrc) {
aud.volume = hSliderValue;
}
}
The problem is that while my Music Volume script only controls the music, for some reason my SFX Volume script controls both the music and the sound effect volumes. So, for example, when I try and mute the sound effects by adjusting the slider the music also becomes muted. Basically I can adjust the music level but can't adjust the sound effects volume without the music also being effected. I'm not sure how to fix this.
Thank you for any answers or feedback - Ben
Answer by Fattie · Jan 28, 2013 at 06:12 AM
"for some reason my SFX Volume script controls both the music and the sound effect volumes"
Hey, I'm pretty sure the issue is you are adjusting the audio
LISTENER !!
note line 9 of your first code fragment.
As a broad general rule, I would probably suggest never, for any reason, adjust audio LISTENERS. Just adjust the volume of your audio sources.
urelatedly:
I'd suggest do NOT do this
function Update (){ AudioListener.volume = hSliderValue/50.0; }
Just adjust it along the lines of your other approach "if it has changed, then adjust it" you know.
Here's some completely typical mixer code, javascript, from a sled ... notice the approach at if ( temp != vv.volume ) ... that's a simple way to do it (not THE most efficient, but fine). I hope it helps!
// allow jokers who need to, to cut the final mix
#pragma strict
var A1:AudioSource;
var A1b:AudioSource;
var A2:AudioSource;
var A3:AudioSource;
var B1:AudioSource;
var B2:AudioSource;
var B3:AudioSource;
var B4:AudioSource;
var B5:AudioSource;
var B502:AudioSource;
var B6:AudioSource;
var B7:AudioSource;
var B8:AudioSource;
var B8b:AudioSource;
var B9:AudioSource;
var B19:AudioSource;
var B12:AudioSource;
var B13:AudioSource;
var B14:AudioSource;
var B15:AudioSource;
var B16:AudioSource;
var B17:AudioSource;
var C1:AudioSource;
var C2:AudioSource;
var C3a:AudioSource;
var C3b:AudioSource;
var C4:AudioSource;
var C5a:AudioSource;
var C5b:AudioSource;
var C6:AudioSource;
var C7:AudioSource;
var D1:AudioSource;
var D2:AudioSource;
var D3:AudioSource;
var D4:AudioSource; // that's the new end sequence, Group3
var D5:AudioSource; // hilarious respaw-like sound
private var voicesEitherObject;
var music:AudioSource;
/////////////////////////////////////////
function SetVolA( vv:float )
{
A1.volume = vv;
A1b.volume = vv;
A2.volume = vv;
A3.volume = vv;
}
function SetVolB( vv:float )
{
B1.volume = vv;
B2.volume = vv;
B3.volume = vv;
B4.volume = vv;
B5.volume = vv;
B502.volume = vv;
B6.volume = vv;
B7.volume = vv;
B8.volume = vv;
B8b.volume = vv;
B9.volume = vv;
B19.volume = vv;
B12.volume = vv;
B13.volume = vv;
B14.volume = vv;
B15.volume = vv;
B16.volume = vv;
B17.volume = vv;
}
function SetVolC( vv:float )
{
C1.volume = vv;
C2.volume = vv;
C3a.volume = vv;
C3b.volume = vv;
C4.volume = vv;
C5a.volume = vv;
C5b.volume = vv;
C6.volume = vv;
C7.volume = vv;
}
function SetVolD( vv:float )
{
D1.volume = vv;
D2.volume = vv;
D3.volume = vv;
D4.volume = vv;
D5.volume = vv;
}
function SetVolMusic( vv:float )
{
music.volume = vv;
}
function SetVolVoice( vv:float )
{
// it is called either VoiceReads or VoiceReadsLocal or some such nonsense
var VRL:GameObject;
VRL = GameObject.Find("VoiceReads");
if ( ! VRL)
{
Debug.Log("could not find VoiceReads .. try the other");
// try one more ...
VRL = GameObject.Find("VoiceReadsLocal");
if ( ! VRL)
{
Debug.Log("could not find VoiceReadsLocal .. giving up");
// do not try again! go home
return;
}
}
//Debug.Log("setting voice volume");
VRL.GetComponent(AudioSource).volume = vv;
}
/////////////////////////////////////////
private var AA;
private var BB;
private var CC;
private var DD;
private var MMM;
private var VVV;
function Start()
{
AA = new Volgroup(SetVolA, 0.35, "A");
BB = new Volgroup(SetVolB, 0.50, "B");
CC = new Volgroup(SetVolC, 0.7, "C");
DD = new Volgroup(SetVolD, 1.0, "D");
MMM = new Volgroup(SetVolMusic, .6, "MM");
VVV = new Volgroup(SetVolVoice, 1, "VV");
}
var bigType:GUIStyle;
class Volgroup
{
var F:Function;
var volume:float;
var name:String;
function Volgroup(ff:Function, initv:float, ss:String)
{
F=ff;
volume=initv;
name=ss;
F(initv);
}
}
private var SW:float;
private var SH10:float;
private var SH50:float;
private var SH80:float;
private var SH90:float;
private var SH95:float;
var cents:int;
private var fadersOn:boolean;
function OnGUI()
{
if (Input.touchCount == 4 )
{ fadersOn = true; return; }
if (Input.touchCount == 5 )
{ fadersOn = false; return; }
if ( ! fadersOn ) return;
SW = Screen.width;
SH10 = Screen.height *.1;
SH50 = Screen.height *.5;
SH80 = Screen.height *.8;
SH90 = Screen.height *.9;
SH95 = Screen.height *.95;
fader( AA, 0.2 );
fader( BB, 0.3 );
fader( CC, 0.4 );
fader( DD, 0.5 );
fader( MMM, 0.6 );
fader( VVV, 0.7 );
}
function fader( vv:Volgroup, pos:float )
{
var temp:float;
pos = SW * pos;
temp = GUI.VerticalSlider (Rect (pos,SH10, 50,SH80), vv.volume, 1,0 );
if ( temp != vv.volume )
{
vv.volume = temp;
vv.F(temp);
}
GUI.Label (Rect (pos+10,SH90-25, 200,50), "0", bigType);
GUI.Label (Rect (pos+10,SH50-25, 200,50), "50", bigType);
GUI.Label (Rect (pos+10,SH10-25, 200,50), "100", bigType);
cents = Mathf.Round(vv.volume * 100);
GUI.Label (Rect (pos+10,SH95-25, 200,50), vv.name+": "+cents, bigType);
}
/////////////////////////////////////////////
Your answer
Follow this Question
Related Questions
Need help with my slider 3 Answers
Multiple, independent volume sliders 1 Answer
Music play throughout all scenes? 1 Answer
AudioListener.volume doesn't change the volume 4 Answers
Volume Slider 0 Answers