x


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

more ▼

asked Jan 28, 2013 at 05:09 AM

ThatBenGuy gravatar image

ThatBenGuy
18 31 24 28

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

"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);
     }
 
 
 /////////////////////////////////////////////


more ▼

answered Jan 28, 2013 at 06:12 AM

Fattie gravatar image

Fattie
27.4k 332 693 417

Thank you very much, your answer helped a lot.

Feb 01, 2013 at 10:49 AM ThatBenGuy
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x6481
x273
x185
x118
x11

asked: Jan 28, 2013 at 05:09 AM

Seen: 1261 times

Last Updated: Feb 01, 2013 at 10:49 AM