Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Why isn't PlayerPrefs.GetInt() not working?

Discussion in 'Documentation' started by PascalGamingYT, Aug 20, 2017.

  1. PascalGamingYT

    PascalGamingYT

    Joined:
    Jul 26, 2017
    Posts:
    5
    I am working on a project where I want to make a Scoreboard scene but before I started, I wanted to know and understand what PlayerPrefs.GetInt() was. After reading the api, I did a test to see if it works. I went to the registry editor on windows, went to my project and added a new string value called Player Name, and I attached an int value of 20, just for the test. I created an int variable called number and I made it public so I can see what value is attached to it when I ran unity. Here is my script:
    Code (CSharp):
    1. public int number;
    2.  
    3. void Start() {
    4.     number = PlayerPrefs.GetInt ("Player Name", 10);
    5. }
    And for some kind of reason, the only number attached to 'number' is 10. Why? I would really appreciate your help. If you have any questions, you can simply ask.
     
    Last edited: Aug 21, 2017
  2. liortal

    liortal

    Joined:
    Oct 17, 2012
    Posts:
    3,562
    Are you storing any value under the key "Player Name" ? if not, you'll always get the default value (10).
     
  3. PascalGamingYT

    PascalGamingYT

    Joined:
    Jul 26, 2017
    Posts:
    5
    Yes, I have stored a value of 20, i guess. Right clicked the key with the name "Player Name" and clicked modify, added a value of 20, which appeared on the right of "Player Name".

    It is kinda like this pic shows, but instead of Icon it's Player Name and On the right under the DATA tab shows up 20, which is the value I had in mind for Player Name. All left are the other binary keys unity has by default in any project you create...I'm guessing.



     
    Last edited: Aug 21, 2017
  4. PJRM

    PJRM

    Joined:
    Mar 4, 2013
    Posts:
    303
    I advice you to do this:
    Code (CSharp):
    1. if (PlayerPrefs.HasKey("Player Name")) {
    2.   // do your code, and get it's value!
    3. } else {
    4.   // make sure you create this value!
    5. }