- Home /
Multiple, independent volume sliders
Hello I'm working on a script that allows the player to adjust both the Music volume and Sound Effects volume mid-game through the use of sliders. I've managed to make the Music volume adjustable but I'm not sure how to do so with the Sound Effects volume. I attached an Audio Source, the Audio Clip named 'Song 1', to the player. Attached to various Item-pickups are Audio Source Clips named 'Pickup5' and 'Pickup2' to be adjusted by the Sound Effects volume slider, if that helps.
The code I'm using is:
GetComponent("Volume - Hard").enabled = false;
var newSkin : GUISkin;
var hSliderValue : float = 25.0;
function OnGUI () {
GUI.Box(Rect(492, 250, 175, 150), "Music");
if(GUI.Button(Rect(550, 365, 65, 30), "Ok")) {
var script1 = GetComponent("Volume - Hard");
script1.enabled = false;
}
hSliderValue = GUI.HorizontalSlider (Rect (520, 280, 120, 30), hSliderValue, 0.0, 50.0);
}
function Update (){
AudioListener.volume = hSliderValue/50.0;
}
I have tried repeating the coding for the Music volume slider again, changing the position of the slider, to be used for the Sound Effects slider but to no avail. Also the 'var newSkin: GUISkin;' is not working. I am able to add a GUISkin but the Custom Skin isn't replacing the default GUISkin. I've used this line of code before but this is the first time there is a problem with it.
Thank you very much for any answers or feedback. -Ben
Answer by fafase · Aug 13, 2012 at 06:12 AM
I don't see the command:
GUI.skin = customSkin;
to place your new skin.
To alter the sound effect volume, you could fetch the Audio Source on the object and reduce the volume from there.
var audioSrc:AudioSource[];
function Start(){
audioSrc= FindObjectsOfType(AudioSource) as AudioSource[];;
for (var aud : AudioSource in audioSrc) {
aud.volume = newValues;
}
}
Here it is in the Start but you would call a similar function when you modify the slider: As I reckon you would modify it when game is quiet (not during a battle) or on pause, the find function which should not be used in Update won't create a glitch.
EDIT: It might be that if you instantiate a new object after altering the volume, it will not get the correct level. Your volume should be a static var that you give the value from the slider in the OnGui script and when instantiating a new object in any script use that var to give it the proper value:
var clone = Instantiate(prefab, position, rotation);
if(clone.audio)
clone.audio.volume = GUIScript.staticVolume;
Here I assume the script is called GUIScript and the variable is called staticVolume and is declared in GUIScript.
GetComponent("Volume - Hard").enabled = false;
var newSkin : GUISkin;
var hSliderValue : float = 25.0;
var previousValue:float= hsliderValue;
var script1:Volume - Hard;
function Start(){
script1 = GetComponent("Volume - Hard");
}
function OnGUI () {
GUI.Box(Rect(492, 250, 175, 150), "Music");
if(GUI.Button(Rect(550, 365, 65, 30), "Ok")) {//Not sure what is going on here
script1.enabled = false;
}
//Slider gives the value
hSliderValue = GUI.HorizontalSlider (Rect (520, 280, 120, 30), hSliderValue, 0.0, 50.0);
// new value is compared to the store value
// if different the function is called to put all level up/down
if(previousValue != hsliderValue)Volume();
// the new value is stored as old value for next frame
previousValue = hsliderValue;
}
// the function that puts all volume up/down
function Volume(){
var audioSrc:AudioSource[];
// Fetch all audio sources and store them in the array declared above
audioSrc= FindObjectsOfType(AudioSource) as AudioSource[];
// Go through each of the audio source and assign the value
for (var aud : AudioSource in audioSrc) {
aud.volume = hSliderValue;
}
}
Have a go and report if it fails, we will look again together hand in hand.
Thank you very much for your answer. I'm a little unsure on how to incorporate the code you provided into my script. Also for some reason 'GUI.skin = customSkin;' doesn't work for me. The console says that 'customSkin' is an 'unknown identifier'
yes because I used the general term, you need to replace customSkin with your own variable, newSkin. One way to implement could be to store the value of the slider and compare it with the new one. If different call the function.I update the answer.
Thank you very much, I really appreciate :). Everything works fine now and the way I need it to work. Just as a reference, in a couple of lines I had to change 'hsliderValue' to 'hSliderValue'. Also what I did now was use the coding you gave me in one script, and the coding I had in my question in another script to have two sliders, one controlling the music and one controlling the sound effects, next to each other.