Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
0
Question by sheffieldlad · Nov 18, 2013 at 05:01 PM · androidplayerprefs

[ANDROID]saving data with playerprefs

Hello.

I'm making an android 2d runner game and I'm trying to save some data periodically such as high scores, total distance run, number of games played, average distance per game etc.

I'm attempting to use PlayerPrefs to do this but the problem is (I think...) that my saved keys are all rest to 0 after the app is closed and restarted.

Here is an example of my game over routine where I attempt to save some data..

 private void GameOver () {
         renderer.enabled = false;
         rigidbody.isKinematic = true;
         totalNumberOfJumps+=numberOfJumps;
         PlayerPrefs.SetFloat ("totalNumberOfJumps", totalNumberOfJumps);
         
         PlayerPrefs.SetFloat ("NumberOfPlays", numberOfPlays);
         
         GUIManager.SetTotalNumberOfJumps(totalNumberOfJumps);
         GUIManager.SetNumberOfPlays(numberOfPlays);
         
         totalDistanceRun += distanceTraveled;
         PlayerPrefs.SetFloat ("totalDistanceTraveled", totalDistanceRun);
         GUIManager.SetTotalDistanceRun(totalDistanceRun);
         GUIManager.SetHighScore(highScore);
         averageDistance = totalDistanceRun / numberOfPlays;
         PlayerPrefs.SetFloat ("averageDistance", averageDistance);
         
         GUIManager.SetAverageDistance(averageDistance);
             
         
         if(score>highScore){
             highScore = score;
             
             PlayerPrefs.SetFloat("HighScore", highScore);
             
             GUIManager.SetHighScore(highScore);
             
             
             Debug.Log (highScore);
             
         PlayerPrefs.Save();
         }
         enabled = false;
         
     }


I then attempt to read the total number of jumps at the start of my game..

 private void GameStart () {
 
 PlayerPrefs.GetFloat ("totalNumberOfJumps", totalNumberOfJumps);
 
 }

My totalNumberOfJumps variable is 0 where before the app was restarted it reflected the true number of jumps.

I can't get it to work in the Unity Editor or on Android.

What's the best way to debug PlayerPrefs on a device? Does PlayerPrefs return an error if you use GetFloat and the key does not exist?

Should I be using PlayerPrefs to store data such as this and if not, what are my alternatives?

I have no idea where PlayerPrefs stores it's data on Droid, I've read of a location but I can't find it on my rooted Galaxy S2. Does anyone know where I can find it?

Using Unity 4.2.1f4, Android 4.0.4

Edited to show full GameOver() function

Comment
Add comment · Show 2
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 Landern · Nov 18, 2013 at 05:05 PM 1
Share

Do you actually save the PlayerPrefs to disk at any point?

 PlayerPrefs.Save() // http://docs.unity3d.com/Documentation/ScriptReference/PlayerPrefs.Save.html
avatar image sheffieldlad · Nov 18, 2013 at 05:31 PM 0
Share

Apologies for being vague. The PlayerPrefs are saved at the very end of my GameOver function of which a snippet is in my post. I'm saving quite a bit of info and most of it has been removed for clarity. I should have included the PlayerPrefs.Save(); line though. I will edit my code to show the full function.

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Yury-Habets · Nov 18, 2013 at 06:39 PM

Hi,

You have to assign your variable to what was returned by GetFloat:

 private void GameStart () {
     totalNumberOfJumps = PlayerPrefs.GetFloat ("totalNumberOfJumps");
 }

Second parameter is the default value assigned if "totalNumberOfJumps" is not set in PlayerPrefs.

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 sheffieldlad · Nov 18, 2013 at 06:50 PM 0
Share

$$anonymous$$any thanks, that works as I wanted it to.

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

19 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

Related Questions

PlayerPrefs not saving 1 Answer

is it safe to save info in PlayerPrefs? 2 Answers

How do I get the Play button to work? 2 Answers

How to load two different scenes from a single UI button using PlayerPrefs 2 Answers

How many different scripts do I need to write to create a highscore system? 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