Playerprefs saving for 1 value but doesnt do it for another

I have Scene Menu and from there i can go to Scene Settings or Game. I have made the slider configuration of sensitivity and volume in Settings. im using playerprefs so it saves the settings(player doesnt have to adjust it everytime) When i go back from Settings to Menu everything is fine(saved) but when i return to settings(or exit application) my sensitivity resets(VOLUME SAVES SUCCESFULLY AND DOESNT HAVE ANY ISSUES). I have spent 3 hours on this double checking everything and i just cant come up with a solution:

```using UnityEngine;
using UnityEngine.UI;
using UnityEngine.UIElements;

public class SettingsMenu : MonoBehaviour
{
[SerializeField] UnityEngine.UI.Slider volumeSlider;
[SerializeField] UnityEngine.UI.Slider sensitivitySlider;

// Start is called once before the first execution of Update after the MonoBehaviour is created
void Awake()
{

    if (!PlayerPrefs.HasKey("masterVolume"))
    {
        PlayerPrefs.SetFloat("masterVolume", 1);
    }
    if (!PlayerPrefs.HasKey("sens"))
    {
        PlayerPrefs.SetFloat("sens", 0.5f);
    }
    Load();
}

// Update is called once per frame
void Update()
{
    Debug.Log(PlayerPrefs.GetFloat("sens"));
}
public void SetFullscreen(bool isFullscreen)
{
    Screen.fullScreen = isFullscreen;
}
public void ChangeVolume()
{
    AudioListener.volume = volumeSlider.value;
    Save();
}
public void ChangeSensitivity()
{
    Save();
}
private void Load()
{
    volumeSlider.value = PlayerPrefs.GetFloat("masterVolume");
    sensitivitySlider.value = PlayerPrefs.GetFloat("sens");
}
private void Save()
{
    PlayerPrefs.SetFloat("masterVolume", volumeSlider.value);
    PlayerPrefs.SetFloat("sens", sensitivitySlider.value);
    PlayerPrefs.Save();
}

}

Just skimmed over the code and it seems fine. The only thing that might be worth checking is ChangeVolume() and ChangeSensitivity(). I’m guessing you’re connecting them in the editor to the onValueChanged event. But I’m sure these methods should include a float arg e.g. ChangeVolume(float volume).

Aside from that it’s possible that something isn’t connected correctly in the editor. Maybe confirm that volumeSlider and sensitivitySlider are set to unique sliders. Maybe also add logs to ChangeVolume() and ChangeSensitivity() to make sure they’re triggering. Probably the best way to test code is with breakpoints.

Normally when I’m using player prefs I find it easy to define a constant for the string keys like “masterVolume” and “sens” to avoid typos (not that I’m seeing any here). Also when posting on the forum try using the code formatting tags.

Most likely the value is read correctly from Prefs (use a Debug.Log or use the debugger) but overwritten by changes made by code following the loading of the value. For a simple example of such a mistake:

// in Awake
Load()

// in OnEnable
sensitivitySlider.value = 1f;

Don’t write that key string every time. Use a const to prevent any typos from sneaking in.

public const string MasterVolume = "masterVolume";

Add that to a PrefsKey class and then use it anywhere in place of the string:

PlayerPrefs.GetFloat(PrefsKey.MasterVolume);

as i said before, maybe i was’nt clear enough, i am using Debug.Log(PlayerPrefs.GetFloat(“sens”)); every update, when i press Back(go to MAIN MENU SCENE) it is still fine(saved value) but once i click on settings again the sensitivity slider resets and debug.log is also shows reseted value(0.5)(BECAUSE BOTH MY DEFAULT SLIDER VALUE AND MY CODE ON AWAKE IS SETTING IT ON 0.5 I I HAVE TRIED COMMENTING THE PART IN THE AWAKE FUNCTION AND IT STILL RESETS) and this is happening only when i go back to settings scene, if i go to game scene my sensitivity works fine(not reseting) this is happening only for sens and not for masterVolume. i added float parameter in my functions, it didnt fix it. I will attach my sliders below:

i have to do it in 2 posts because im new member and cant attach more than 1 image per post:

Also, what did you mean by this?

image

I tried adding SFX volume and Music volume and it doesnt work, only master volume works and i have spent so much time to figure out what is different and cant find anything…

That is strange. As far as I can see everything looks like its configured correctly. My current gut feeling is that there might be another script that’s accessing the player prefs e.g. the bug could be in the main menu scene. Maybe try adding a breakpoint to every PlayerPrefs.SetFloat(). Although I can’t see it being used, also make sure there isn’t a PlayerPrefs.DeleteAll() anywhere.

Although it probably wont help with your current bug the get method also has a shorthand for default values:

volumeSlider.value = PlayerPrefs.GetFloat(PrefsKey.MasterVolume, 1);

I fixed it.

the problem was that when scene loads, event OnValueChanged on sliders hooks because it sets the default slider values(from slider component). i added isLoading bool and it’s fixed. Havent i mentioned how much i love Unity?

using UnityEngine;
using UnityEngine.UI;
using UnityEngine.UIElements;
using UnityEditor.Audio;
using UnityEngine.Audio;

public class SettingsMenu : MonoBehaviour
{
    [SerializeField] public UnityEngine.UI.Slider masterVolumeSlider,        musicVolumeSlider, SFXVolumeSlider;
    [SerializeField] public UnityEngine.UI.Slider sensitivitySlider;
    [SerializeField] public AudioMixer mainAudioMixer;

    private string audioMixerMaster = "audioMixerMasterVolume";
    private string audioMixerMusic = "audioMixerMusicVolume";
    private string audioMixerSFX = "audioMixerSFXVolume";

    private string prefsMasterVolume = "masterVolume";
    private string prefsMusicVolume = "musicVolume";
    private string prefsSFXVolume = "sfxVolume";
    private string prefsSensitivity = "playerSensitivity";

    private bool isLoading = false;

    // Start is called once before the first execution of Update after the MonoBehaviour is created
    void Start()
    {

        if (!PlayerPrefs.HasKey(prefsMasterVolume))
        {
            PlayerPrefs.SetFloat(prefsMasterVolume, 1);
        }
        if (!PlayerPrefs.HasKey(prefsMusicVolume))
        {
            PlayerPrefs.SetFloat(prefsMusicVolume, 1);
        }
        if (!PlayerPrefs.HasKey(prefsSFXVolume))
        {
            PlayerPrefs.SetFloat(prefsSFXVolume, 1);
        }
        if (!PlayerPrefs.HasKey(prefsSensitivity))
        {
            PlayerPrefs.SetFloat(prefsSensitivity, 1);
        }
        Load();
    }

    // Update is called once per frame
    void Update()
    {
        Debug.Log(PlayerPrefs.GetFloat(prefsSensitivity));
    }
    public void SetFullscreen(bool isFullscreen)
    {
        Screen.fullScreen = isFullscreen;
    }
    public void ChangeMasterVolume()
    {
        if (isLoading) return; // ignore events during load
        SetMixerVolume(audioMixerMaster, masterVolumeSlider.value);
        Save();
    }
    public void ChangeMusicVolume()
    {
        if (isLoading) return; // ignore events during load
        SetMixerVolume(audioMixerMusic, musicVolumeSlider.value);
        Save();
    }
    public void ChangeSFXVolume()
    {
        if (isLoading) return; // ignore events during load
        SetMixerVolume(audioMixerSFX, SFXVolumeSlider.value);
        Save();
    }
    public void ChangeSensitivity()
    {
        if (isLoading) return; // ignore events during load
        Save();
    }
    private void Load()
    {
        isLoading = true;

        masterVolumeSlider.value = PlayerPrefs.GetFloat(prefsMasterVolume);
        musicVolumeSlider.value = PlayerPrefs.GetFloat(prefsMusicVolume);
        SFXVolumeSlider.value = PlayerPrefs.GetFloat(prefsSFXVolume);
        sensitivitySlider.value = PlayerPrefs.GetFloat(prefsSensitivity);


        SetMixerVolume(audioMixerMaster, masterVolumeSlider.value);
        SetMixerVolume(audioMixerMusic, musicVolumeSlider.value);
        SetMixerVolume(audioMixerSFX, SFXVolumeSlider.value);

        isLoading = false;
    }
    private void Save()
    {
        PlayerPrefs.SetFloat(prefsMasterVolume, masterVolumeSlider.value);
        PlayerPrefs.SetFloat(prefsMusicVolume, musicVolumeSlider.value);
        PlayerPrefs.SetFloat(prefsSFXVolume, SFXVolumeSlider.value);
        PlayerPrefs.SetFloat(prefsSensitivity, sensitivitySlider.value);
        PlayerPrefs.Save();
    }
    private void SetMixerVolume(string parameterName, float sliderValue)
    {
        float dB = Mathf.Log10(Mathf.Clamp(sliderValue, 0.0001f, 1f)) * 20f;
        mainAudioMixer.SetFloat(parameterName, dB);
    }
}