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

Example of using Player Prefs to save and get a variable.

Discussion in 'iOS and tvOS' started by TheChronicPhenix, Mar 1, 2010.

  1. TheChronicPhenix

    TheChronicPhenix

    Joined:
    Jan 14, 2010
    Posts:
    874
    I've read all over that I should use Player Prefs for saving my game, but I don't know how to go about that, could someone post an example script of saving a variable using Player Prefs, and then accessing it again? Thanks, any help would be greatly apprieciated.
     
  2. bigkahuna

    bigkahuna

    Joined:
    Apr 30, 2006
    Posts:
    5,434
  3. sigsom

    sigsom

    Joined:
    Aug 9, 2009
    Posts:
    133
    You will also want to encrypt any data you store in there because it will get hacked and will mess up leaderboards etc. You could also write a class that stores variables from the class as a deliminated string and parse it out and encrypt so you only have to make 2 methods. Such as Store and Retrieve methods that call private encrypt and decrypt methods. Then all you have to do is invoke that class to handle your loading and saving and point references to the variables instead of retrieving from playerPrefs every time you want the data.

    Also remember not to use strings as your keys and instead store them for future use. IE it is better to use a static
    Code (csharp):
    1. var key = "PLAYER VALUE KEY";
    than to use
    Code (csharp):
    1. PlayerPrefs.GetString("KEY");
    because it will create and destroy a string which will activate the GC.
     
  4. TheChronicPhenix

    TheChronicPhenix

    Joined:
    Jan 14, 2010
    Posts:
    874
    I would go about encrypting them, but I'm not going to include leaderboards in my game. Partially because I have no idea how to add them. Thanks for the examples and info, it really helped.
     
  5. sigsom

    sigsom

    Joined:
    Aug 9, 2009
    Posts:
    133
    You can pretty easily use the AGON or OpenFeint API code. There is ample documentation on this forum and on the development forums of those API's about getting them to work with Unity.
     
  6. Warp boy2

    Warp boy2

    Joined:
    Nov 12, 2010
    Posts:
    216
    i have a question of my own about player prefs if may..

    can i mentionate then in if statements?

    for example , a save and load feature.. can i do this?

    Code (csharp):
    1.  
    2.  
    3. var checkpoint: float;
    4.  
    5. PlayerPrefs.SetFloat("stage", 1);
    6.  
    7.  
    8.  
    for saving the last location the player was before he closed the aplication and.. this:

    Code (csharp):
    1.  
    2.  
    3. switch (checkpoint) {
    4.  
    5. case 0: "teleport to map 0"
    6.  
    7. break;
    8.  
    9. case 1: "teleport to map 1"
    10.  
    11. break;
    12.  
    13. }
    14.  
    15.  
    16.  
    for the load game ? would this work?
     
  7. Jasper-Flick

    Jasper-Flick

    Joined:
    Jan 17, 2011
    Posts:
    957
    Sure, that works. But use an int for that, not a float.
     
    MedMekss likes this.
  8. Warp boy2

    Warp boy2

    Joined:
    Nov 12, 2010
    Posts:
    216
    thanks! you rock dude.. ROCK!
     
    FALA likes this.
  9. Warp boy2

    Warp boy2

    Joined:
    Nov 12, 2010
    Posts:
    216
    i tried but in the load game script.. its says that it has no idea what "checkpoint" is
     
    DaniyarGilymov likes this.
  10. Frank Oz

    Frank Oz

    Joined:
    Oct 13, 2010
    Posts:
    1,560
    If the variable checkpoint is in another script, don't you have to put something to tell it which script to look in? (I'm guessing).
     
  11. Warp boy2

    Warp boy2

    Joined:
    Nov 12, 2010
    Posts:
    216
    hmm.. well i guess i could use the static comand on the var checkpoint.. but that still didnt do anything.. he aways read the var as zero value

    how can i connect the playerpref with this checkpoint var?
     
  12. CoCoNutti

    CoCoNutti

    Joined:
    Nov 30, 2009
    Posts:
    513
    Can you rephrase that all again in English for non-programmer / programming noobs like me? LOL
     
  13. unityart3ds

    unityart3ds

    Joined:
    Oct 31, 2012
    Posts:
    3
    Dont call your self a noob that just beating your self up
     
    jipsen likes this.
  14. Utopians

    Utopians

    Joined:
    Dec 4, 2013
    Posts:
    2
  15. tejash_vt

    tejash_vt

    Joined:
    Oct 21, 2020
    Posts:
    1
    Thanks,This helped me after 10 years.
     
    unity_-_IxKPb71_BLAA likes this.