PlayerPrefs bug?

I wrote this code to reload saved settings for sliders at start, if there is no save settings, then it reloads default values. Then there is also a reset settings function, which I assigned to a button in the UI.

    void LoadSettings()
    {
        if (PlayerPrefs.HasKey("MouseSensitivity")) mouseSens.value = PlayerPrefs.GetFloat("MouseSensitivity");
        else mouseSens.value = 8f;

        sensitivityValueText.text = mouseSens.value.ToString();

        if (PlayerPrefs.HasKey("MasterVolume")) masterVolume.value = PlayerPrefs.GetFloat("MasterVolume");
        else masterVolume.value = 0f;
        mixer.SetFloat("Master Volume", masterVolume.value);

        if (PlayerPrefs.HasKey("MusicVolume")) musicVolume.value = PlayerPrefs.GetFloat("MusicVolume");
        else musicVolume.value = 0f;
        mixer.SetFloat("Music Volume", musicVolume.value);

    }

    public void ResetSettings()
    {
        PlayerPrefs.DeleteAll();
        LoadSettings();
    }

I call LoadSettings() Function on OnEnable() function.
The weird thing is, the mouse sensitivity part is working perfectly, but the music and master volume part does not work. When I press reset button for the first time, the mouse sens part works perfectly again, but the rest doesn`t work. However, if I press reset button a second time, then everything works fine. What could be causing this? Thanks.

Did you try to run it with debugger and actually see what happens?

You are using different keys:
GetFloat("MasterVolume")
SetFloat("Master Volume", masterVolume.value)
Put string in a static/const variable and use it to avoid such typos.

1 Like

One is for audio mixer, the other for playerprefs. OnSliderChange function which is not up there, playerprefs sets ā€œMasterVolumeā€ key. I checked spellings many times, problem is not there unfortunately. But you re right I should have used more clear strings.

Im not sure about how to use debugger exactly but Ill try to learn that thank you.

Edit: This solved half of the problem

    public void OnMouseSensChange(float value)
    {
        sensitivityValueText.text = value.ToString();
        PlayerPrefs.SetFloat("MouseSensitivity", value);
        if(GameObject.FindGameObjectWithTag("Player") != null) GameObject.FindGameObjectWithTag("Player").GetComponent<Player>().speed = value;
    }

I didn`t check if the player reference is null (in main menu it is null), I just added this. Now music and master audio sliders reloads their saved or default values, but audio mixer values does not change at start until I press reset or change sliders. I used debug.log after each else statement to see if code keeps going and code does not stop anywhere.

If you are sure that the parameter names are correct in PlayerPrefs.GetFloat/PlayerPrefs.SetFloat (which is not in your code) then obviously it is connected to the mixer ā€œfeatureā€.

Values cannot be changed in Awake/OnEnable. Change it in Start or do delays.

1 Like

Oh that makes sense now thank you so much.

Yeah, this bigtime… don’t splatter random string literals all over your code!!

Instead, wrap it up nice and tight and neat.

Here’s an example of simple persistent loading/saving values using PlayerPrefs:

https://pastebin.com/icJSq5zC

Useful for a relatively small number of simple values.

2 Likes