Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

PlayerPrefs Location and Lifecycle

Discussion in 'Editor & General Support' started by SteveJ, Apr 5, 2011.

  1. SteveJ

    SteveJ

    Joined:
    Mar 26, 2010
    Posts:
    3,085
    Can anyone tell me how PlayerPrefs work within the editor environment? Where are they stored for each project, how long do they live, etc?

    I'm trying to get something working where my objects remember the state they were in when a player moves back and forth between levels.

    I'm doing something like this when a door is opened (for example):

    Code (csharp):
    1.  
    2. PlayerPrefs.SetInt(persistence_ref.stage + "_" + objectID + "_dooropen", 1);
    3.  
    And then in that door's Start() function:

    Code (csharp):
    1.  
    2.  if (PlayerPrefs.HasKey(persistence_ref.stage + "_" + objectID + "_dooropen"))
    3.  {
    4.   if (PlayerPrefs.GetInt(persistence_ref.stage + "_" + objectID + "_dooropen") == 1)
    5.   {
    6.    Debug.Log("Opening " + objectID);
    7.    OpenDoor();
    8.   }
    9.  }
    10.  
    But none of this works - nothing happens at all (the Debug.Log never fires). The door's default state when the level loads is closed.

    To test, I'm running the game and opening the door. Then stopping the game. The running it again and returning to the level in question... but the door is closed :(

    Can't figure out if my code is bad or I'm misunderstanding how PlayerPrefs work. I can't locate the actual PlayerPrefs file to look at it so I'm not sure if it's even writing it.
     
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Depends on the platform; on OS X they're in ~/Library/Preferences, on Windows they're in the registry. They don't expire. (You don't really need HasKey and GetInt like that; just use GetInt.)

    --Eric
     
  3. SteveJ

    SteveJ

    Joined:
    Mar 26, 2010
    Posts:
    3,085
    Yeah the documentation says this:

    Sorry - I literally had that page open and was looking through it, but "%APPDATA%" jumped out at me (from the Webplayer) and I just went looking. Should have read more carefully.

    Ahh okay - "If it doesn't exist, it will return defaultValue."... so I could just do this:

    Code (csharp):
    1.  
    2. if (PlayerPrefs.GetInt(persistence_ref.stage + "_" + objectID + "_dooropen", 0) == 1)
    3.  
    Actually, it looks like the default default (!) might be zero anyway so it can just be left out entirely.

    And thanks - this has help me resolve the problem. The persistence_ref.stage variable wasn't being established before this door code was firing. I needed to move that into an Awake() function on it's object and that fixed everything.

    Now my doors retain their state and it's absolutely awesome... :)