Search Unity

PlayerPrefs menu settings reset when returnting to scene

Discussion in 'Scripting' started by GaryCassette, Feb 18, 2018.

  1. GaryCassette

    GaryCassette

    Joined:
    Oct 1, 2016
    Posts:
    1
    Greetings,

    My first post ever in Unity forums, yay!

    I have created a settings menu system where you can choose game difficulty from easy to hard.
    Setttings is in scene called "Settings" and you can return to main menu scene from settings scene. Main menu is in Scene called Main

    Issue is that when choosing difficulty in settings scene and going to main menu and then going back to settings scene it seems that difficulty setting is reset back to 0 and I can't figure why...

    Could someone much wiser check what's wrong?

    Thanks!

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class SettingsManager : MonoBehaviour {
    7.  
    8.     public Text difficultytext;
    9.     public int ballSpeed;
    10.     int difficulty;
    11.  
    12.     private void Awake()
    13.     {
    14.         difficulty = PlayerPrefs.GetInt("Difficulty");
    15.     }
    16.  
    17.     private void Start()
    18.     {
    19.         print("Playerprefs Difficulty" + (PlayerPrefs.GetInt("difficulty")));
    20.     }
    21.  
    22.     public void BallSpeedSetting()
    23.     {
    24.         difficulty++; //Every mousebutton press increases difficulty setting by one.
    25.         if (difficulty > 2)
    26.         {
    27.             difficulty = 0; //loops back to easy from hard.
    28.         }
    29.  
    30.         switch (difficulty)
    31.         {
    32.             case 0:
    33.                 difficultytext.text = ("Easy");
    34.                 difficultytext.color = Color.green;
    35.                 PlayerPrefs.SetInt("BallSpeed", 8);
    36.                 PlayerPrefs.SetInt("Difficulty", 0);
    37.                 print(PlayerPrefs.GetInt("Difficulty"));
    38.                 break;
    39.  
    40.             case 1:
    41.                 difficultytext.text = ("Medium");
    42.                 difficultytext.color = Color.yellow;
    43.                 PlayerPrefs.SetInt("BallSpeed", 13);
    44.                 PlayerPrefs.SetInt("Difficulty", 1);
    45.                 print(PlayerPrefs.GetInt("Difficulty"));
    46.                 break;
    47.  
    48.             case 2:
    49.                 difficultytext.text = ("Hard");
    50.                 difficultytext.color = Color.red;
    51.                 PlayerPrefs.SetInt("BallSpeed", 17);
    52.                 PlayerPrefs.SetInt("Difficulty", 2);
    53.                 print(PlayerPrefs.GetInt("Difficulty"));
    54.                 break;
    55.  
    56.             default:
    57.                 Debug.Log("Difficulty setting is something else than Easy,Medium or Hard.");
    58.                 break;
    59.         }
    60.     }
    61. }
    62.  
     
  2. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Make sure you spell it the same (the playerprefs key)
    You capitalized 1 and not the other.
     
  3. takatok

    takatok

    Joined:
    Aug 18, 2016
    Posts:
    1,496
    This appears to be the code some UI element (a button?) is calling. Your incrementing your difficulty and changing the color of the text, etc. You seem to be saving the difficulty just fine. Is there any code on the difficultytext GameObject that looks up your playerprefs in an an Awake() function.

    Just because your changing the difficulty in this script and saving it to playerprefs, you still need to be accessing playerprefs in your other scripts and loading those values in. Do you have that code to post?
     
  4. User340

    User340

    Joined:
    Feb 28, 2007
    Posts:
    3,001
    I find it good habit to use constants for pref keys. That way there is no concern about misspelling.

    Code (CSharp):
    1. const string healthKey = "health";
    2.  
    3. PlayerPrefs.SetInt(healthKey, 10);
    4. var health = PlayerPrefs.GetInt(healthKey);
     
    CrandellWS likes this.