Search Unity

Player prefs not saving on game restart

Discussion in 'Scripting' started by Nayan2017, Jun 18, 2019.

  1. Nayan2017

    Nayan2017

    Joined:
    May 13, 2019
    Posts:
    13
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5. using UnityEngine.SceneManagement;
    6.  
    7. public class ShopControlScript : MonoBehaviour {
    8.  
    9.  
    10.     private int index;
    11.     float moneyAmount;
    12.     float isRifleSold;
    13.  
    14.     public Text moneyAmountText;
    15.     public Text riflePrice;
    16.     public GameObject riflee;
    17.     public Button buyButton;
    18.  
    19.     // Use this for initialization
    20.     void Start () {
    21.         index = PlayerPrefs.GetInt("CharacterSelected");
    22.         moneyAmount = PlayerPrefs.GetFloat ("MoneyAmount");
    23.  
    24.        isRifleSold = PlayerPrefs.GetFloat("IsRifleSold");
    25.  
    26.         if (PlayerPrefs.HasKey("IsRifleSold"))
    27.         {
    28.             if (isRifleSold == 1)
    29.                 buyRifle();
    30.  
    31.             if (moneyAmount >= 5 && isRifleSold == 0)
    32.                 buyButton.interactable = true;
    33.             else
    34.                 buyButton.interactable = false;
    35.         }
    36.     }
    37.  
    38.     // Update is called once per frame
    39.     void Update () {
    40.  
    41.  
    42.         if (isRifleSold == 1)
    43.             riflee.SetActive(false);
    44.         else
    45.             riflee.SetActive(true);
    46.  
    47.  
    48.         moneyAmountText.text = "Money: " + moneyAmount.ToString() + "$";
    49.  
    50.         isRifleSold = PlayerPrefs.GetFloat("IsRifleSold");
    51.  
    52.         if (moneyAmount >= 5 && isRifleSold == 0)
    53.             buyButton.interactable = true;
    54.         else
    55.             buyButton.interactable = false;  
    56.     }
    57.  
    58.     public void buyRifle()
    59.     {
    60.         moneyAmount -= 5;
    61.         PlayerPrefs.SetFloat("IsRifleSold", 1);
    62.         riflePrice.text = "UNLOCKED";
    63.         buyButton.gameObject.SetActive (false);
    64.         PlayerPrefs.Save();
    65.     }
    66.  
    67.     public void exitShop()
    68.     {
    69.         PlayerPrefs.SetInt("CharacterSelected", index);
    70.         PlayerPrefs.SetFloat("MoneyAmount", moneyAmount);
    71.         PlayerPrefs.Save();
    72.         SceneManager.LoadScene ("World1");
    73.  
    74.     }
    75.  
    76.     public void resetPlayerPrefs()
    77.     {
    78.         moneyAmount = 0;
    79.         buyButton.gameObject.SetActive (true);
    80.         riflePrice.text = "Price: 5$";
    81.         PlayerPrefs.DeleteAll ();
    82.     }
    83.  
    84.  
    85.  
    86. }
    87.  
     
  2. csofranz

    csofranz

    Joined:
    Apr 29, 2017
    Posts:
    1,556
    How do you know that it does not save? Perhaps some other script is calling resetPlayerPrefs()? Try disabling that first, and then read back Player prefs upon start
     
  3. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,188
    As pointed out, print out the values of your playerprefs. Also, look into a playerpref editor viewer on the asset store. There are free ones.

    Now, that being said, don't use floats when you need an int. IsRifleSold is setup as a float, but using an int would be better since you're just using it as a bool tracker. You might set a float to == 0, but it may actually come out 0.00059 for example. So it may never actually equal 0.