Hello,
I have been reading through other questions regarding this and was able to get the sound to change between scenes but when going back to the main menu where the volume slider is located, the volume slider is back to full and the slider no longer changes the volume. How can I save the position of the slider and change the sound from this point?
Any help appreciated, Thank you.
If you don’t use DontDestroyOnLoad() the the audio volume will always start at the same level when loading a scene.
What you want to do instead is save the volume level set by the slider to PlayerPrefs and get this value on loading a scene. So you can do something like the script below
This would be on the game scenes themselves
// you may want to add the RequireComponent() attribute shown below to this script
// [RequireComponent(typeof(AudioSource), typeof(AudioSource))]
AudioSource audioSource;
void Start ()
{
audioSource = GetComponent<AudioSource>();
// get the float value of SliderVolumeLevel if it has been saved with PlayerPrefs.SetFloat()
// else use defult value of audioSource.volume
audioSource.volume = PlayerPrefs.GetFloat("SliderVolumeLevel", audioSource.volume);
}
this would be added to the script with the Slider on the main menu.
float volume = 0.5f; // AudioSource.volume will have a value 0.0f to 1.0f
void SaveSliderValue()
{
PlayerPrefs.SetFloat("SliderVolumeLevel", volume);
}