Home > Software engineering >  How to Fix Save and Load Play Prefs
How to Fix Save and Load Play Prefs

Time:02-03

I have a settings menu that has the option for a volume, viewdistance, and quality sliders. I want to save the value of those and use them when the player loads into the game. My issue is the quality level and view distance reset while the volume saves properly.

private void Start()
    {
        if(!PlayerPrefs.HasKey("musicVolume"))
        {
            PlayerPrefs.SetFloat("musicVolume",1);
            Load();
        }
        else 
        {
            Load();
        }
        if (!PlayerPrefs.HasKey("qualityLevel"))
        {
            PlayerPrefs.SetInt("qualityLevel",6);
            Load();
            
        }
        
        else 
        {
            Load();
        }
        if (!PlayerPrefs.HasKey("ViewDistance"))
        {
            PlayerPrefs.SetFloat("ViewDistance", 10);
            Load();
            Debug.Log("saved viewdistance"   qualityslider.value * 1000);
        }
        else 
        {
            Load();
        }
    }
private void Save()
    {
        
        PlayerPrefs.SetFloat("musicVolume", volumeslider.value);
        PlayerPrefs.SetInt("qualityLevel", QualitySettings.GetQualityLevel());
        PlayerPrefs.SetFloat("ViewDistance", qualityslider.value);
    }
private void Load()
    {
        volumeslider.value = PlayerPrefs.GetFloat("musicVolume");
        qualityslider.value = PlayerPrefs.GetFloat("ViewDistance");
         QualitySettings.SetQualityLevel(PlayerPrefs.GetInt("qualityLevel"));
    }
public void ChangeVolume ()
     
    {
        Save();
        AudioListener.volume = volumeslider.value;

    }
public void ChangeViewDistance()
{
    Save();
    Camera.main.farClipPlane  = viewDistanceSlider.value * 1000;

}
 public void ChangeQuality(float level)
 {
    //level = qualityslider.value;
      
           
           // QualitySettings.SetQualityLevel(level, false);
           
           switch((float)level)
      {

         case 0:
     QualitySettings.SetQualityLevel(0); // For lowest quality
     break;
     case 1:
     QualitySettings.SetQualityLevel(1); // For lowest quality
     break;
     case 2:
     QualitySettings.SetQualityLevel(2); // For lowest quality
     break;
     case 3:
     QualitySettings.SetQualityLevel(3); // For lowest quality
     break;
     case 4:
     QualitySettings.SetQualityLevel(4); // For lowest quality
     break;
     case 5:
     QualitySettings.SetQualityLevel(5); // For lowest quality
     break;
     case 6:
     QualitySettings.SetQualityLevel(6); // For lowest quality
     break;
      }
       
}



This is my first time trying save and load with player prefs in unity. I recieve no errors and can't find anything online. The volume saves but the view distance and quality do not.

UI

UI

ChangeQuality

Change View Distance Change Volume

CodePudding user response:

Your ChangeVolume() function calls Save() but your ChangeQuality() function does not.

CodePudding user response:

Well, firstly,

You check if the key exists and if you don't set the value, you can just use PlayerPrefs.GetFloat("YourKey", DefaultValue) because when the player will need a different value they'll just change it in the settings and the new value will be read.

Secondly, you don't need to always update the preferences, only when they are changed, i. e. you change volume and save only volume, it's easier to write and read.

Thirdly, in ChangeQuality(float level) you can just do QualitySettings.SetQualityLevel(level) and use Mathf.Clamp(level, 0, 6) if you wanna restirct with the range from 0 to 6.

After doing that check if you call ChangeViewDistance() and ChangeQuality(), that's probably the whole cause of the problem.

CodePudding user response:

To save the changed value of a slider to PlayerPrefs your public method must look like that

public void ChangeQuality(System.Single value) => PlayerPrefs.SetFloat("qualityLevel", Mathf.Clamp(value, 0, 6));

You need to use System.Single because it will help Unity automatically pass the new value, so you can save it.

Your method in the list must be under 'Dynamic float' label

enter image description here

  • Related