Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

PlayerPrefs Dont Save?

Discussion in 'Scripting' started by xtechadvanced, Nov 15, 2015.

  1. xtechadvanced

    xtechadvanced

    Joined:
    Aug 25, 2013
    Posts:
    8
    I have used playerprefs before and they have always worked no problem but for some reason when i try to use them now they don't save, any insight is appreciated

    they work until the game closes it seems to reset the prefs.


    Code (CSharp):
    1.         if (PlayerPrefs.GetInt("Kills")== null){
    2.  
    3.                 PlayerPrefs.SetInt("Kills",1);
    4.  
    5.             }else{
    6.  
    7.                 PlayerPrefs.SetInt("Kills",PlayerPrefs.GetInt("Kills") + 1);
    8.  
    9.             }
    10.  
    11.             PlayerPrefs.Save();
     
  2. Onufriyev

    Onufriyev

    Joined:
    Oct 30, 2015
    Posts:
    4
    Try storing the value kills in a temp var.
     
  3. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,380
    you do know that 'GetInt' will never return null, because int can never be null. It's a value type, value types can't be null.
     
  4. larku

    larku

    Joined:
    Mar 14, 2013
    Posts:
    1,422
    Replace that with

    Code (CSharp):
    1. if (PlayerPrefs.GetInt("Kills", 0)==0) {
    2.     PlayerPrefs.SetInt("Kills",1);
    3. } else {
    4.     PlayerPrefs.SetInt("Kills",PlayerPrefs.GetInt("Kills") + 1);
    5. }
    6.  
    7. PlayerPrefs.Save();
    Or even simpler, replace the whole lot with just.
    Code (csharp):
    1. PlayerPrefs.SetInt("Kills",PlayerPrefs.GetInt("Kills",0) + 1);
    2. PlayerPrefs.Save();
     
  5. xtechadvanced

    xtechadvanced

    Joined:
    Aug 25, 2013
    Posts:
    8
    Thanks but that didn't work :(
     
  6. larku

    larku

    Joined:
    Mar 14, 2013
    Posts:
    1,422
    "didn't work" as in?
     
  7. xtechadvanced

    xtechadvanced

    Joined:
    Aug 25, 2013
    Posts:
    8
    Didn't work as in it works until the game closes and reopens it is back at zero
     
  8. Onufriyev

    Onufriyev

    Joined:
    Oct 30, 2015
    Posts:
    4
    What platform?
     
  9. xtechadvanced

    xtechadvanced

    Joined:
    Aug 25, 2013
    Posts:
    8
    PC Standalone