How can i keep the same sound settings across different scenes?,How do i save the volume for sound into a different scene?
So I'm making a game and in my main menu I have a menu that has buttons and one of those is a sound setting button that pulls a panel with sliders that control the sound for the background music and sound effects. However, for some reason when you press play and move to the next scene and use the pause menu where I can access another sound settings panel the sound settings just revert as if you were playing the game for the first time and the volume shoots up to the default settings. I want to know how I can have the same sound settings saved across all scenes even if I have different songs playing.
This is the code I used for the sound settings in the main menu:
public class AudioManager : MonoBehaviour
{
private static readonly string FirstPlay = "FirstPlay";
private static readonly string BackGroundPref = "BackGroundPref";
private static readonly string SoundEffectsPref = "SoundEffectsPref";
private int FirstPlayInt;
public Slider BackGroundMusic, SoundEffects;
private float F_BackGroundMusic, F_SoundEffects;
public AudioSource BackMusic;
public AudioSource[] VFX;
void Awake()
{
FirstPlayInt = PlayerPrefs.GetInt(FirstPlay);
if (FirstPlayInt == 0)
{
F_BackGroundMusic = .25f;
F_SoundEffects = .75f;
BackGroundMusic.value = F_BackGroundMusic;
SoundEffects.value = F_SoundEffects;
PlayerPrefs.SetFloat(BackGroundPref, F_BackGroundMusic);
PlayerPrefs.SetFloat(SoundEffectsPref, F_SoundEffects);
PlayerPrefs.SetInt(FirstPlay, -1);
}
else
{
F_BackGroundMusic = PlayerPrefs.GetFloat(BackGroundPref);
BackGroundMusic.value = F_BackGroundMusic;
F_SoundEffects = PlayerPrefs.GetFloat(SoundEffectsPref);
SoundEffects.value = F_SoundEffects;
}
}
public void SaveSoundSettings()
{
PlayerPrefs.SetFloat(BackGroundPref, BackGroundMusic.value);
PlayerPrefs.SetFloat(SoundEffectsPref, SoundEffects.value);
}
private void OnApplicationFocus(bool focus)
{
if (!focus)
{
SaveSoundSettings();
}
}
public void UpdateSound()
{
BackMusic.volume = BackGroundMusic.value;
for (int i = 0; i < VFX.Length; i++)
{
VFX[i].volume = SoundEffects.value;
}
}
}
Your answer
Follow this Question
Related Questions
Audio Cutting Out Unexplainably 1 Answer
Audio is cut in script 0 Answers
Cracking at end of audio? 0 Answers
Play audio clip over another audio source 1 Answer
Need help: new sound isnt played correctly anymore 2 Answers