How to save volume when switching scene?
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.
public static AudioManager Instance;
void Awake()
{
if(Instance)
DestroyImmediate(gameObject);
else
{
DontDestroyOnLoad(gameObject);
Instance = this;
}
}
public void MusicVolume(float musicControl)
{
if (FindObjectOfType<AudioManager>())
{
GetComponent<AudioSource>().volume = musicControl;
}
}
}
It might be your $$anonymous$$enu code. It should read the volume from your Audio$$anonymous$$anager to initialize.
In your above code I don't understand why you have that if statement in the $$anonymous$$usicVolume method. You would not be able to call the method unless the Audio$$anonymous$$anager existed.
Your also never calling your $$anonymous$$usicVolume function - why not just temperately store the volumes value in a PlayerPrefs SetFloat and then use GetFloat after on the next scene load? $$anonymous$$ight be a little bit easier,and that way you dont have to handle not destroying any objects from scene to scene, its all stored in the code.
I added the if statement there because i was getting duplicate audiomanagers when going back to my main menu.
I mean this if statement.
if (FindObjectOfType-Audio$$anonymous$$anager-())
It checks if there is any Audio$$anonymous$$anager before setting the volume. So if there is 1 or 8 it returns true, but it can never return false because the method exists within an Audio$$anonymous$$anager.
Answer by TBruce · Jun 21, 2016 at 06:01 PM
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);
}
Thank you, Not sure i understand. If i don't use DontDestroyOnLoad then how do i get the audio$$anonymous$$anger to Instansiate in each scene? Or would i add an Audio$$anonymous$$anger to every scene? When i try that the sound just resets to full volume on each scene.
Personally I would not make Audio$$anonymous$$anger static in the first place. And yes, I would add Audio$$anonymous$$anger to every scene.
Having said that if you save the "SliderVolumeLevel" in in the main menu using PlayerPrefs. You can then load it as shown in the Start() function above as seen below.
// this statement either sets audioSource.volume to the PlayerPrefs SliderVolumeLevel if it exists or
// it leaves it at the default value
audioSource.volume = PlayerPrefs.GetFloat("SliderVolumeLevel", audioSource.volume);
Answer by ConsoleHack000 · Nov 04, 2016 at 05:42 PM
There are many ways to do this. One is PlayerPrefs, another one is a static variable, or you can use dontdestroyonload.