Hello,
Long time reader, first time questioner here. None of the documents I have found seem to address this exact issue, but I’m sure that’s on me. After 6 hours on this, I’m ready to break down and ask for help.
I have a main menu. That main menu has 2 audio options sliders that set the Music and Sound volume levels. When I change the slider settings, PlayerPrefs are updated and the volume levels change. Everything works fine when I go to game scene. However, the problem occurs when I go back to the main menu. When I return to the Main Menu, the Music Volume PlayerPref resets to the default level of 0.5 instead of remaining at the current PlayerPref level. The Sound PlayerPref is working properly and remains at the current level.
The only thing I can think of is that the Music volume is set in the Update function in my Sound Manager script whereas the Sound volume is changed elsewhere. I change the Music level in Update as it is always playing.
Some things I have already done on top of multiple different scripting option is debugging the MusicVolume PlayerPref and deleting (to reset) the PlayerPrefs.
Basically I am trying to figure out why the MusicVolume PlayerPref is being reset and how to stop that from happening as I move between scenes. Thank you for your help.
Attached are the relevant scripts:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class AudioVolume : MonoBehaviour {
public UIElements uiElements = new UIElements();
[System.Serializable]
public class UIElements
{
public Slider soundSlider; //Drag a reference to the slider which will control the sound effects.
public Slider musicSlider; //Drag a reference to the slider which will control the music.
}
void Awake ()
{
SetSliders(); //Calls function to set slider level to PlayerPrefs
}
public void GetSliders ()
{
PlayerPrefs.SetFloat("MusicVolume", uiElements.musicSlider.value); //Sets Music volume when slider changes
PlayerPrefs.SetFloat("SoundVolume", uiElements.soundSlider.value); //Sets Sound volume when slider changes
SavePlayerPrefs();
}
public void SetSliders ()
{
uiElements.soundSlider.value = PlayerPrefs.GetFloat("SoundVolume"); //Sets Sound slider position to SoundVolume PlayerPrefs setting
uiElements.musicSlider.value = PlayerPrefs.GetFloat("MusicVolume"); //Sets Music slider position to MusicVolume PlayerPrefs setting
}
public void SavePlayerPrefs()
{
PlayerPrefs.Save(); //Saves PlayerPrefs levels
}
}
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class SoundManager : MonoBehaviour
{
public AudioSource efxSource; //Drag a reference to the audio source which will play the sound effects.
public AudioSource musicSource; //Drag a reference to the audio source which will play the music.
public static SoundManager instance = null; //Allows other scripts to call functions from SoundManager.
public float lowPitchRange = .98f; //The lowest a sound effect will be randomly pitched.
public float highPitchRange = 1.02f; //The highest a sound effect will be randomly pitched.
void Awake ()
{
//Check if there is already an instance of SoundManager
if (instance == null)
//if not, set it to this.
instance = this;
//If instance already exists:
else if (instance != this)
//Destroy this, this enforces our singleton pattern so there can only be one instance of SoundManager.
Destroy (gameObject);
//Set SoundManager to DontDestroyOnLoad so that it won't be destroyed when reloading our scene.
DontDestroyOnLoad (gameObject);
}
void Update ()
{
musicSource.volume = PlayerPrefs.GetFloat ("MusicVolume"); //Changes the Music Volume
}
//Used to play single sound clips.
public void PlaySingle(AudioClip clip)
{
//Set the clip of our efxSource audio source to the clip passed in as a parameter.
efxSource.clip = clip;
//Play the clip.
efxSource.Play ();
}
//RandomizeSfx chooses randomly between various audio clips and slightly changes their pitch.
public void RandomizeSfx (params AudioClip[] clips)
{
//Generate a random number between 0 and the length of our array of clips passed in.
int randomIndex = Random.Range(0, clips.Length);
//Choose a random pitch to play back our clip at between our high and low pitch ranges.
float randomPitch = Random.Range(lowPitchRange, highPitchRange);
//Set the pitch of the audio source to the randomly chosen pitch.
efxSource.pitch = randomPitch;
efxSource.volume = PlayerPrefs.GetFloat ("SoundVolume");
//Set the clip to the clip at our randomly chosen index.
efxSource.clip = clips[randomIndex];
//Play the clip.
efxSource.Play();
}
}