- Home /
how to change and save audio volume from a different scene with a slider
im trying to make an options menu for a game that has a slider for changing the master volume for the whole game. i have a audio source on the main menu that plays the music over all the scenes but do not know how to access that audio source to change and then save that volume when changing between the other menu screens. any help would be much appreciated.
Answer by gvergidis · Aug 21, 2018 at 12:24 PM
Hello. Your options should be persistant saved.
So, when a player sets the volume with the slider and hits Apply (or Save button whatever you name it), you should store this value to playerprefs. Try this to save this value :
PlayerPrefs.SetFloat("Options_VolumeLevel", SliderObject.GetComponent<Slider>().value);
This single code of line will save the slider's value to your prefs location (Register for Windows, android->data for android etc).
Not, when you change your scene, on your initialize method, before started playing your music, retrieve this value to change your AudioSource volume like this :
AudioSourceObject.GetComponent<AudioSource>().volume = PlayerPrefs.GetFloat("Options_VolumeLevel", 1f);
This second value is the default value if the key does not exist.
Do the same when the user opens the options panel to change slider value to corresponding value. Hope that helped.
Forgot to mention. Sliders have a OnValueChanged listener which means that every time the value changes, a function is called. Create a function to update the AudioSource volume simultaneously with your slider for better results.
Your answer
![](https://koobas.hobune.stream/wayback/20220612172806im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Volume Slider 0 Answers
cannot implicitly convert type 'int' to 'bool' problem 1 Answer
Sound effects too quiet 8 Answers
Is there a way to limit the output volume on an audio mixer group? 1 Answer