Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
6
Question by Bryangs · Mar 12, 2017 at 08:50 PM · playerprefsfloatstorehow-to

How to use PlayerPrefs

Hey guys, i started to work on a simple game yesterday, and it's almost done, but i wanted to implement a Shop for the Player to buy things. I already made a system to store how many points the Player has, but i don't know how to save a float after the game is closed, and then restore it once it opens. I've been reading a little, and i discovered that there is one way, and you gotta use PlayerPrefs, but i never used it, and i didn't get what am i supposed to do and how. Could someone explain to me how would i use PlayerPrefs to save a variable once the game is closed, and then restore it once it is opened again?

Thank you kindly =]

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image SnStarr · Mar 13, 2017 at 02:14 AM 0
Share

Do something like..

     float playerPoints = 100;
     
     void SavePoints()
     {
     PlayerPrefs.SetFloat( "PlayerPoints", playerPoints );
     }
 
 void LoadPoints()
 {
 PlayerPrefs.GetFloat("PlayerPoints");
 }

And access these methods through GUI buttons that save/load...But keep in $$anonymous$$d..Player prefs is for saving data such as Settings and player name and etc...The player can easily "hack" the player prefs file and change any value they want. I would reccommend saving and loading all data with X$$anonymous$$L Serialization.

4 Replies

· Add your reply
  • Sort: 
avatar image
8

Answer by Utopians · Apr 28, 2020 at 12:47 PM

Writing:

 PlayerPrefs.SetInt("score",5);
 PlayerPrefs.SetFloat("volume",0.6f);
 PlayerPrefs.SetString("username","John Doe");
 PlayerPrefs.Save();
 

// If you need boolean value:

 bool val = true;
 PlayerPrefs.SetInt("PropName", val ? 1 : 0);
 PlayerPrefs.Save();

Reading:

 int score = PlayerPrefs.GetInt("score");
 float volume = PlayerPrefs.GetFloat("volume");
 string player = PlayerPrefs.GetString("username");
 
 bool val = PlayerPrefs.GetInt("PropName") == 1 ? true : false;








Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
7

Answer by Commoble · Mar 13, 2017 at 01:25 AM

Playerprefs is meant for saving player preferences; you wouldn't want to use it to save high scores and progress data, because it's not difficult for players to edit the playerprefs data.

There's a couple of ways to save data safely, and this official unity tutorial video goes over one of them, using C#'s BinaryFormatter.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
2

Answer by Alexei_UA · Mar 13, 2017 at 06:57 AM

At start of your game you use PlayerPrefs.GetInt("Player level"); to restore your player level (or something else).

Than you play your game and....

On disable() you use PlayerPrefs.SetInt("Player level", currentLevel); to write you current value (player level etc.).

It is an example of using PlayerPrefs with simple player level up system:

 public class ExperienceLevelSystem : MonoBehaviour {
     
         public static int currentXP;
     
         private void Start()
         {
             if (PlayerPrefs.HasKey("currentXP"))
             {
                 currentXP = PlayerPrefs.GetInt("currentXP");
             }
     
             Updated();
         }
     
         private void Update()
         {
             if (currentXP >= maxXP)
             {
                 LevelUpSystem();
             }
     
             Updated();
         }
     
         public void LevelUpSystem()
         {
             //currentXP = currentXP - maxXP;
             maxXP = 200 + 200 * (currentLevel * currentLevel);
             currentLevel++;
             levelUpMessage.SetActive(true);
             Time.timeScale = 0;
     
         public static void Add(int pointsToAdd)
         {
             currentXP += pointsToAdd;
         }
     
         void OnDisable()
         {
             PlayerPrefs.SetInt("currentLevel", currentLevel);
         }
 
 











Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image SnStarr · Mar 13, 2017 at 07:02 AM 2
Share

$$anonymous$$eep in $$anonymous$$d however, in this example..A player can easily find the file where the player prefs were saved and modify it easily to make themselves max level with no effort..Its standard practice to use player prefs only for player preferences such as Game Settings, $$anonymous$$ouse Bindings...ETC..Things a player would already have access to modify.

avatar image
0

Answer by SuryaPrakashModi · Aug 17, 2020 at 02:06 PM

https://www.youtube.com/watch?v=GcnERRQ6K8Q

i think this video will be helpfull

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

73 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Change store balanced based on level scores. 1 Answer

performance wise, which is better..Playerprefs or global floats 3 Answers

Store clicks in sequence 1 Answer

How to use PlayerPrefs to save to floats (Sensitivity Help) 0 Answers

Noob to Shaders - How to check for PlayerPref 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges