- Home /
PlayerPrefs Resets Automatically upon returning to the Main Menu
My main menu has the slider. But when i switch to the next scene and return back to the main menu, my PlayerPref value does not work. I want the PlayerPrefs value to be saved when I return back from the next scene. Can someone please help me?
Here is my code on the Main Menu :
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;
public static AudioManager _instance;
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, soundSlider.value);
PlayerPrefs.SetFloat(SoundPref, soundSlider.value);
}
void OnApplicationFocus(bool inFocus)
{
if (!inFocus)
{
SaveSoundSettings();
}
else
{
UpdateSound();
}
}
public void UpdateSound()
{
musicAudio.volume = musicSlider.value;
for (int i = 0; i < soundAudio.Length; i++)
{
soundAudio[i].volume = soundSlider.value;
}
}
void Awake()
{
//if we don't have an [_instance] set yet
if(!_instance)
_instance = this ;
//otherwise, if we do, kill this thing
else
Destroy(this.gameObject) ;
DontDestroyOnLoad(this.gameObject) ;
}
}
and this my code for the next 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;
void Awake()
{
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 Jasr_88 · Jun 05, 2020 at 04:15 PM
The first time you run your game the key FirstPlay dosent exist, so will never be equal to 0, insted of:
if (firstPlayInt == 0)
Try with:
if (!PlayerPrefs.HasKey(FirstPlay))
Hope it helps!
Answer by Rebekah-D-David · Jun 05, 2020 at 05:11 PM
@Jasr_88 Thanks for your help but sadly that doesn't seem to work either. I've got an error: ========================================
NullReferenceException: Object reference not set to an instance of an object AudioManager.SaveSoundSettings () (at Assets/Scripts/AudioManager.cs:43) AudioManager.OnApplicationFocus (System.Boolean inFocus) (at Assets/Scripts/AudioManager.cs:51) UnityEditorInternal.InternalEditorUtility:OnGameViewFocus(Boolean) UnityEditor.HostView:OnLostFocus() (at C:/buildslave/unity/build/Editor/Mono/HostView.cs:170)
well, it's working, but you have an other error that i can only assume is: you are not setting the sound Slider property in your inspector or the reference to the property got lost. Please double check the value assigned to the soundSlider property in your inspector.
I find the properties for my script in the inspector missing while the scene is switched during the gameplay. How can I fix this ?
The components on the GameObjects are not persistent between scenes, if you load an other scene all the GameObjects are destroyed, and the new ones are loaded. That might cause the impresion that the values ate getting lost, i reccomend you to look up the DontDestroyOnLoad method, examples and it's usage, but that topic is way too far of the scope for this question.