- Home /
Why does my slider properties go missing when switching scenes ?
My slider properties are missing when i switch scene from the main menu. Why does that happen ? Here's my code on the main menu where my Slider is:
using UnityEngine;
using UnityEngine.UI;
public class AudioManager : MonoBehaviour
{
private static readonly string FirstPlay = "FirstPlay";
private static readonly string MusicPref = "MusicPref";
private static readonly string SoundPref = "SoundPref";
private int firstPlayInt;
public Slider musicSlider, soundSlider;
private float musicFloat, soundFloat;
public AudioSource musicAudio;
public AudioSource[] soundAudio;
private static AudioManager instance = null;
void Start()
{
firstPlayInt = PlayerPrefs.GetInt(FirstPlay);
if (firstPlayInt == 0)
{
musicFloat = .125f;
soundFloat = .75f;
musicSlider.value = musicFloat;
soundSlider.value = soundFloat;
PlayerPrefs.SetFloat(MusicPref, musicFloat);
PlayerPrefs.SetFloat(SoundPref, soundFloat);
PlayerPrefs.SetInt(FirstPlay, -1);
}
else
{
musicFloat = PlayerPrefs.GetFloat(MusicPref);
musicSlider.value = musicFloat;
soundFloat = PlayerPrefs.GetFloat(SoundPref);
soundSlider.value = soundFloat;
}
}
public void SaveSoundSettings()
{
PlayerPrefs.SetFloat(MusicPref, musicSlider.value);
PlayerPrefs.SetFloat(SoundPref, soundSlider.value);
}
void OnApplicationFocus(bool inFocus)
{
if (!inFocus)
{
SaveSoundSettings();
}
}
public void UpdateSound()
{
musicAudio.volume = musicSlider.value;
for (int i = 0; i < soundAudio.Length; i++)
{
soundAudio[i].volume = soundSlider.value;
}
}
private void Awake()
{
if (instance == null)
{
instance = this;
DontDestroyOnLoad(gameObject);
return;
}
if (instance == this) return;
{
Destroy(gameObject);
}
}
}
and here's my code on the other scene :
using UnityEngine;
public class AudioSetting : MonoBehaviour
{
private static readonly string MusicPref = "MusicPref";
private static readonly string SoundPref = "SoundPref";
private float musicFloat, soundFloat;
public AudioSource musicAudio;
public AudioSource[] soundAudio;
private void Start()
{
Destroy(FindObjectOfType<AudioSetting>());
}
private void Awake()
{
ContinueSettings();
}
private void ContinueSettings()
{
musicFloat = PlayerPrefs.GetFloat(MusicPref);
soundFloat = PlayerPrefs.GetFloat(SoundPref);
musicAudio.volume = musicFloat;
for(int i = 0; i < soundAudio.Length; i++)
{
soundAudio[i].volume = soundFloat;
}
}
}
Answer by Bunny83 · Jun 06, 2020 at 09:51 AM
Well since this is a pseudo singleton class you are carrying it with you to the next scene. Are your sliders child objects of that singleton or at least on another singleton that is marked with DontDestroyOnLoad? If not of course your slider gameobject will be destroyed when you change scenes.
Apart from that note that you should call PlayerPrefs.Save()
after you made some changes in order to actually flush those changes to disk. Otherwise if your application is terminated those changes might not be saved.
Finally instead of using that "FirstPlayInt" you could just use a default value in your get methods when you read then in the first place. That way your start method would just turn into:
musicFloat = PlayerPrefs.GetFloat(MusicPref, 0.125f);
soundFloat = PlayerPrefs.GetFloat(SoundPref, 0.75f);
musicSlider.value = musicFloat;
soundSlider.value = soundFloat;
Though I usually use this approach: You initialize your variables with a default value in the field initializer and just use GetFloat like this:
musicFloat = PlayerPrefs.GetFloat(MusicPref, musicFloat);
So you only overwrite the value if it has been saved, otherwise it will keep it's initial value. That way the user could delete individual settings (on windows with regedit) and on the next run only that one value would get its default value while the others stay.
@Bunny83 How do I mark my child objects of the singleton with DontDestroyOnLoad. Can you help me with that? I tried many different methods. I just don;t understand what is not working..