Search Unity

I can't figure out why this Save System wouldn't work...

Discussion in 'Scripting' started by hylox, Sep 28, 2020.

  1. hylox

    hylox

    Joined:
    Sep 28, 2020
    Posts:
    6
    I recently started working on my first game and I could solve all the problems except for this one. I tried saving the Settings you could change in my settingsmenu when eg. restarting the game using playerprefs and it all terribly failed. I made the Settingsmenu using Brackeys tutorial. WHen I run this code it just does not save any values and the Resolutions in the Resolutionsdropdown aren't even shown anymore.
    Anyways, I spent 20 hours trying to fix it and I couldn't get it to work so I would be so thankful for your help. here is the code in a bit simplified (sorry for not putting up an exact piece of code but I really do not know what is malfunctioning):
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5. using UnityEngine.Audio;
    6.  
    7. public class SettingsMenu : MonoBehaviour
    8. {
    9.     Resolution[] resolutions;
    10.     private bool isFulls;
    11.  
    12.     public AudioMixer AudioMixer;
    13.     public Dropdown ResolutionDropdown;
    14.  
    15.     public Dropdown DifficultyDropdown;
    16.     public Dropdown QualityDropdown;
    17.     public Toggle FullscreenToggle;
    18.     public Slider volS;
    19.  
    20.     void Awake()
    21.     {
    22.         // Refreshes the shown values
    23.         DifficultyDropdown.value = PlayerPrefs.GetInt("saveDifficultyStory");
    24.         QualityDropdown.value = PlayerPrefs.GetInt("saveQuality");
    25.         volS.value = PlayerPrefs.GetFloat("volume");
    26.  
    27.         // Refreshes the Toggle
    28.         int saveFulls = PlayerPrefs.GetInt("saveFulls");
    29.         if (saveFulls == 0)
    30.         {
    31.             FullscreenToggle.isOn = false;
    32.             isFulls = false;
    33.         }
    34.         if (saveFulls == 1)
    35.         {
    36.             FullscreenToggle.isOn = true;
    37.             isFulls = true;
    38.         }
    39.  
    40.         // Refreshes the option
    41.         QualitySettings.SetQualityLevel(PlayerPrefs.GetInt("saveQuality"));
    42.         AudioMixer.SetFloat("volume", PlayerPrefs.GetFloat("volume"));
    43.  
    44.     }
    45.  
    46.     void Start()
    47.     {
    48.         int CurrentResolutionIndex = 0;
    49.         resolutions = Screen.resolutions;
    50.  
    51.         ResolutionDropdown.ClearOptions();
    52.  
    53.         List<string> options = new List<string>();
    54.  
    55.         for (int i = 0; i < resolutions.Length; i++)
    56.         {
    57.             string Option = resolutions[i].width + " x " + resolutions[i].height + " " + resolutions[i].refreshRate + "Hz";
    58.             options.Add(Option);
    59.  
    60.             if (resolutions[i].width == Screen.currentResolution.width &&
    61.                 resolutions[i].height == Screen.currentResolution.height &&
    62.                 resolutions[i].refreshRate == Screen.currentResolution.refreshRate)
    63.             {
    64.                 CurrentResolutionIndex = i;
    65.             }
    66.         }
    67.  
    68.         ResolutionDropdown.AddOptions(options);
    69.         ResolutionDropdown.value = CurrentResolutionIndex;
    70.         ResolutionDropdown.RefreshShownValue();
    71.     }
    72.  
    73.     public void SetResolution(int ResolutionIndex)
    74.     {
    75.         Resolution resolution = resolutions[ResolutionIndex];
    76.         Screen.SetResolution(resolution.width, resolution.height, Screen.fullScreen);
    77.     }
    78.  
    79.     public void SetVolume(float volume)
    80.     {
    81.         AudioMixer.SetFloat("volume", volume);
    82.         PlayerPrefs.SetFloat("volume", volume);
    83.         PlayerPrefs.Save();
    84.     }
    85.  
    86.     public void SetQuality(int qualityIndex)
    87.     {
    88.         QualitySettings.SetQualityLevel(qualityIndex);
    89.         PlayerPrefs.SetInt("saveQuality", qualityIndex);
    90.         PlayerPrefs.Save();
    91.     }
    92.  
    93.  
    94.     public void SetFullscreen(bool isFullscreen)
    95.     {
    96.         Screen.fullScreen = isFullscreen;
    97.         if (isFulls == true)
    98.         {
    99.             PlayerPrefs.SetInt("saveFulls", 0);
    100.             isFulls = false;
    101.         }
    102.         if (isFulls == false)
    103.         {
    104.             PlayerPrefs.SetInt("saveFulls", 1);
    105.             isFulls = true;
    106.         }
    107.     }
    108.  
    109.     public void SetStoryDifficulty(int diffultyIndex)
    110.     {
    111.         DifficultyManager.difficultyStory = diffultyIndex;
    112.         PlayerPrefs.SetInt("saveDifficultyStory", diffultyIndex);
    113.         PlayerPrefs.Save();
    114.     }
    115. }
     
    Last edited: Sep 28, 2020
  2. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,605
    This would be a first step: http://plbm.com/?p=220
    Explain your problem. People generally dont want to look through a code example without even knowing what to look for. That's just a waste of time :)
     
  3. hylox

    hylox

    Joined:
    Sep 28, 2020
    Posts:
    6
    Thank you for your reply, I was a bit stupid and changed things up a bit ;) I just cannot figure out an exact spot of the code that is wrong so there is this massive codeblock here. I still hope I made it a bit easier to understand what I want the code to do.
     
  4. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,605
    This should save and load the values properly. Check if it really does not by printing one of the values.
    Code (CSharp):
    1. Debug.Log(PlayerPrefs.GetFloat("volume"));
    See if the changes you apply really are not loaded.

    If they are not, check if you do not have any script wiping the PlayerPrefs, for example by calling the DeleteKey function. I assume you do not have any errors in your console?
    https://docs.unity3d.com/ScriptReference/PlayerPrefs.html

    Also, i got to ask: you are actually calling said functions, like SetVolume, with (for example) a UI slider, right? If they are not called, of course no changes would be saved. You can check that they are being called as expected by also putting a simple Debug.Log message into the functions.
     
  5. hylox

    hylox

    Joined:
    Sep 28, 2020
    Posts:
    6
    Thanks, will try that!
     
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,742
    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, such as volume or what-have-you.
     
  7. hylox

    hylox

    Joined:
    Sep 28, 2020
    Posts:
    6
    I wanted to ask if the problem could be that I have too many PlayerPrefs-variables. I have about 60 in total.
     
  8. adehm

    adehm

    Joined:
    May 3, 2017
    Posts:
    369
    I believe you can have as many keys as you want. I prefer to build my save string with delimiters using a single key; might be beneficial for you to learn that approach as it would require one get and one set.
     
  9. hylox

    hylox

    Joined:
    Sep 28, 2020
    Posts:
    6
    Do you know a tutorial for that?
     
  10. adehm

    adehm

    Joined:
    May 3, 2017
    Posts:
    369
  11. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,909
    The only limit is your imagination. And storage space.
     
    hylox likes this.
  12. hylox

    hylox

    Joined:
    Sep 28, 2020
    Posts:
    6
    I found a solution for my original problem. I made a scene just for the settings and it somehow works although i did not not touch these variables in any other script in the original main menu scene. I have no clue why that worked but it did. I think though that getting into what you've send me is beneficial for the future so... thanks, i will check that out!
    :);) thats what I like most about Programming and Game-making in general!