Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Abysinian · Mar 10, 2016 at 06:58 PM · androidplayerprefssaveonapplicationpauseonapplicationquit

PlayerPrefs not Saving Properly on Android (Works in Editor) - OnApplicationPause/Quit

I'm currently developing a mobile game for Android and have run in to an annoying problem.

I'm using PlayerPrefs to save various game state variables and in the editor this works and runs fine regardless of whether it's done via a save button, OnApplicationPause or OnApplicationQuit. However, when I build the .apk and put it on my tablet for testing, the Save call in OnApplicationPause and/or Quit never seem to run, so when I tap the home button and close the app the data is not saved. The issue isn't with the Save method itself, because I set up a temporary button to run the Save method and if I use that before closing the app the data is all correctly saved.

I was wondering if anyone had experienced anything similar and had any idea as to how I can force OnApplicationPause/Quit to happen or for the app not to close when swiped away until the Save method has finished?

Save method:

 public void SaveData()
     {
         PlayerPrefs.SetInt("credits", credits);
         PlayerPrefs.SetInt("crystal", crystal);
         PlayerPrefs.SetInt("dust", dust);
         PlayerPrefs.SetInt("lumber", lumber);
         PlayerPrefs.SetInt("metal", metal);
         PlayerPrefs.SetInt("quantum", quantum);
         PlayerPrefs.SetInt("cRegCap", currentRegDailyCap);
         PlayerPrefs.SetInt("cQCap", currentQuantumDailyCap);
         PlayerPrefs.SetInt("regCap", regDailyCap);
         PlayerPrefs.SetInt("qCap", quantumDailyCap);
         PlayerPrefs.SetInt("regTick", regularTickAmount);
         PlayerPrefs.SetInt("qTick", quantumTickAmount);
         PlayerPrefs.SetString("timeClosed", System.DateTime.UtcNow.ToBinary().ToString());
         PlayerPrefsX.SetIntArray("packs", packs);
         PlayerPrefsX.SetBoolArray("cards", cards);
         PlayerPrefsX.SetIntArray("collectors", collectors);
         PlayerPrefs.Save();
     }

OnApplicationPause/Quit:

 void OnApplicationPause(bool pauseStatus)
 {
     paused = pauseStatus;
 }
 
 void OnApplicationQuit()
 {
     SaveData();
 }
 
 void Update()
 {
     if(paused)
     {
         SaveData();
     }
 }
Comment
Add comment
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

1 Reply

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

Answer by phil_me_up · Mar 11, 2016 at 12:03 AM

The first thing I'd suggest is to call SaveData() from within OnApplicationPause and not wait for the next Update call (which may not happen).

 void OnApplicationPause(bool pauseStatus)
  {
      if(pauseStatus)
           SaveData();
  }
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 Abysinian · Mar 12, 2016 at 04:32 AM 0
Share

Ah, this is working, thank you! I had what you have there previously, but without the bool pauseStatus and the if statement and it wasn't working, so I had ruled out doing it this way. Thanks again.

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

60 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

Related Questions

Loading a save/playerprefs from a different game? 2 Answers

PlayerPrefs don't load again after Android game closes for the first time. 1 Answer

Playerprefs not working on android 0 Answers

Android/IOS: Sharing save file between free and paid versions 0 Answers

How to run Android service behind UnityPlayerActivity? 0 Answers


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