PlayerPrefs.SetInt() default value not working.


I’ve been working on saving data in my game but there is one huge problem I’ve come across. Basically whenever I set an int or a float or anything else and the key hasn’t been set yet it returns zero regardless of whether or not I set a default variable. I ran a test variable and set it to a unmade key as follows:

 public int test;

 void Awake() 
      test = PlayerPrefs.SetInt("testagcfsvd", 1);

the variable “test” reads 0, despite the key never having been created or even saved.

I know there’s got to be something I’m doing wrong but I’ve been at this for hours. Has something been changed in a recent update or am I just making on obvious mistake?

I’m not sure if that’s a typo in your question or your original code but the code you have in your question wouldn’t even compile SetInt doesn’t have something like a default value, only GetInt.

So this line:

test = PlayerPrefs.SetInt("testagcfsvd", 1);

should be

test = PlayerPrefs.GetInt("testagcfsvd", 1);

Note that GetInt will only return the default value of 1 when “testagcfsvd” hasn’t been set yet.