Search Unity

Strange PlayerPref Behavior

Discussion in 'Scripting' started by HarleyK, May 9, 2017.

  1. HarleyK

    HarleyK

    Joined:
    Mar 27, 2015
    Posts:
    95
    So I'm trying to use playerprefs for basic score for several levels, options and so on, which has been working really well. However I'm having strange behavior when trying to do a basic int for lives. Here are the scripts.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.SceneManagement;
    4.  
    5. public class PlayerPrefsManager : MonoBehaviour
    6. {
    7.  
    8.     const string MASTER_VOLUME_KEY = "master_volume";
    9.     const string HIGH_SCORE1 = "High Score1";
    10.     const string HIGH_SCORE2 = "High Score2";
    11.     const string HIGH_SCORE3 = "High Score3";
    12.     const string HIGH_SCORE4 = "High Score4";
    13.     const string SQUISH_LIVES = "Squish Lives";
    14.  
    15.     public static void SetMasterVolume(float volume)
    16.     {
    17.         if (volume >= 0f && volume <= 1f)
    18.         {
    19.             PlayerPrefs.SetFloat("master_volume", volume);
    20.         }
    21.         else
    22.         {
    23.             Debug.LogError("Master Volume out of range");
    24.         }
    25.     }
    26.  
    27.     public static float GetMasterVolume()
    28.     {
    29.         return PlayerPrefs.GetFloat(MASTER_VOLUME_KEY);
    30.     }
    31.  
    32.     public static void SetLives(int lives)
    33.     {
    34.         PlayerPrefs.SetInt(SQUISH_LIVES, lives);
    35.     }
    36.  
    37.     public static int GetLives()
    38.     {
    39.         return PlayerPrefs.GetInt(SQUISH_LIVES);
    40.     }
    41.  
    42.     public static void SetHighScore(int score)
    43.     {
    44.         PlayerPrefs.SetInt(HIGH_SCORE1, score);
    45.     }
    46.  
    47.     public static int GetHighScore()
    48.     {
    49.         return PlayerPrefs.GetInt(HIGH_SCORE1);
    50.     }
    51.  
    52.     public static void SetHighScore2(int score)
    53.     {
    54.         PlayerPrefs.SetInt(HIGH_SCORE2, score);
    55.     }
    56.  
    57.     public static int GetHighScore2()
    58.     {
    59.         return PlayerPrefs.GetInt(HIGH_SCORE2);
    60.     }
    61.  
    62.     public static void SetHighScore3(int score)
    63.     {
    64.         PlayerPrefs.SetInt(HIGH_SCORE3, score);
    65.     }
    66.  
    67.     public static int GetHighScore3()
    68.     {
    69.         return PlayerPrefs.GetInt(HIGH_SCORE3);
    70.     }
    71.  
    72.     public static void SetHighScore4(int score)
    73.     {
    74.         PlayerPrefs.SetInt(HIGH_SCORE4, score);
    75.     }
    76.  
    77.     public static int GetHighScore4()
    78.     {
    79.         return PlayerPrefs.GetInt(HIGH_SCORE4);
    80.     }
    81. }
    Then here is the LivesManager script i thought about using to subtract from lives in the gamemanager script im using, and setting the int and gettting the int.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class LivesManager : MonoBehaviour {
    7.  
    8.     private bool isOver = false;
    9.  
    10.     public Text livesText;
    11.  
    12.     // Use this for initialization
    13.     void Start () {
    14.         isOver = false;
    15.         PlayerPrefsManager.GetLives();
    16.         livesText.text = "x" + PlayerPrefsManager.GetLives();
    17.     }
    18.    
    19.     // Update is called once per frame
    20.     void Update () {
    21.  
    22.         if (GameController.instance.gameOver == true && !isOver)
    23.         {
    24.             LivesCounter();
    25.         }
    26.     }
    27.  
    28.     void LivesCounter()
    29.     {
    30.         GameController.instance.lives--;
    31.         PlayerPrefsManager.SetLives(GameController.instance.lives);
    32.         isOver = true;
    33.     }
    34. }
    35.  
    I'm basically trying to subtract from the lives count till the player runs out of lives, then if the player wishes to continue playing they have to play a reward ad to get more lives. But its not keeping track of the lives count the way it is suppose to which is odd.
     
  2. laxbrookes

    laxbrookes

    Joined:
    Jan 9, 2015
    Posts:
    235
    You aren't saving your changes to disk.

    Code (csharp):
    1.  
    2. PlayerPrefs.Save();
    3.  
    ..is required.
     
  3. HarleyK

    HarleyK

    Joined:
    Mar 27, 2015
    Posts:
    95
    Yeah I tried that but am having the same issue it seems
     
  4. laxbrookes

    laxbrookes

    Joined:
    Jan 9, 2015
    Posts:
    235
    Can you debug log the lives before and after you attempt to subtract?
     
  5. ayrrik

    ayrrik

    Joined:
    Sep 17, 2015
    Posts:
    1
    In your LivesManager.Start method, you are not setting lives variable.. only getting it, but not storing it.. you need:
    GameController.instance.lives = PlayerPrefsManager.GetLives();