Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 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
0
Question by LiamBradley · Apr 03 at 10:35 AM · playerprefs

Why is PlayerPrefs.DeleteAll() not working?

So in my code I'm using playerprefs to save and load data between scenes, but I want all of the data to be erased when I close out of the game. For this I'm using OnApplicationQuit, which through Debog.Log I've proved gets called when I close the game.

 public void OnDisable()
     {
         PlayerPrefs.SetInt("canUseSpell", canUseSpell);
         PlayerPrefs.SetInt("spd1", spd1SetActive);
         PlayerPrefs.SetInt("spd2", spd2SetActive);
         PlayerPrefs.SetInt("spd3", spd3SetActive);
         PlayerPrefs.SetInt("maxHP1", MaxHp1SetActive);
         PlayerPrefs.SetInt("maxHP2", MaxHp2SetActive);
         PlayerPrefs.SetInt("maxMP1", MaxMp1SetActive);
         PlayerPrefs.SetInt("maxMP2", MaxMp2SetActive);
         PlayerPrefs.SetInt("HpRegen1", HPRegen_1SetActive);
         PlayerPrefs.SetInt("HpRegen2", HPRegen_2SetActive);
         PlayerPrefs.SetInt("MpRegen1", MPRegen_1SetActive);
         PlayerPrefs.SetInt("MpRegen2", MPRegen_2SetActive);
         PlayerPrefs.SetInt("Dex1", Dexterity_1SetActive);
         PlayerPrefs.SetInt("Dex2", Dexterity_2SetActive);
         PlayerPrefs.SetInt("Def1", Defense_1SetActive);
         PlayerPrefs.SetInt("Def2", Defense_2SetActive);
         PlayerPrefs.SetInt("Def3", Defense_3SetActive);
         PlayerPrefs.SetFloat("moveSpeed", moveSpeed);
         PlayerPrefs.SetInt("HPperSec", HPperSec);
         PlayerPrefs.SetInt("MPperSec", MPperSec);
         PlayerPrefs.SetFloat("Dex", fireSpeed);
         PlayerPrefs.SetInt("Def", Defense);
         PlayerPrefs.SetInt("level", level);
         PlayerPrefs.SetFloat("HP", Hitpoints);
         PlayerPrefs.SetFloat("MaxHP", MaxHitpoints);
         PlayerPrefs.SetFloat("MP", Mana);
         PlayerPrefs.SetFloat("MaxMP", MaxMana);
         PlayerPrefs.SetInt("upgradePoints", upgradePoints);
         PlayerPrefs.SetInt("xp", experience);
         PlayerPrefs.SetInt("xpToNextLvl", experienceToNextLevel);
     }
     public void OnEnable()
     {
         canUseSpell = PlayerPrefs.GetInt("canUseSpell", 0);
         spd1SetActive = PlayerPrefs.GetInt("spd1", 0);
         spd2SetActive = PlayerPrefs.GetInt("spd2", 0);
         spd3SetActive = PlayerPrefs.GetInt("spd3", 0);
         MaxHp1SetActive = PlayerPrefs.GetInt("maxHP1", 0);
         MaxHp2SetActive = PlayerPrefs.GetInt("maxHP2", 0);
         MaxMp1SetActive = PlayerPrefs.GetInt("maxMP1", 0);
         MaxMp2SetActive = PlayerPrefs.GetInt("maxMP2", 0);
         HPRegen_1SetActive = PlayerPrefs.GetInt("HpRegen1", 0);
         HPRegen_2SetActive = PlayerPrefs.GetInt("HpRegen2", 0);
         MPRegen_1SetActive = PlayerPrefs.GetInt("MpRegen1", 0);
         MPRegen_2SetActive = PlayerPrefs.GetInt("MpRegen2", 0);
         Dexterity_1SetActive = PlayerPrefs.GetInt("Dex1", 0);
         Dexterity_2SetActive = PlayerPrefs.GetInt("Dex2", 0);
         Defense_1SetActive = PlayerPrefs.GetInt("Def1", 0);
         Defense_2SetActive = PlayerPrefs.GetInt("Def2", 0);
         moveSpeed = PlayerPrefs.GetFloat("moveSpeed", 1);
         HPperSec = PlayerPrefs.GetInt("HPperSec", 1);
         MPperSec = PlayerPrefs.GetInt("MPperSec", 1);
         fireSpeed = PlayerPrefs.GetFloat("Dex", 0.3f);
         Defense = PlayerPrefs.GetInt("Def", 0);
         level = PlayerPrefs.GetInt("level", 0);
         Hitpoints = PlayerPrefs.GetFloat("HP", 800);
         MaxHitpoints = PlayerPrefs.GetFloat("MaxHP", 800);
         Mana = PlayerPrefs.GetFloat("MP", 50);
         MaxMana = PlayerPrefs.GetFloat("MaxMP", 50);
         upgradePoints = PlayerPrefs.GetInt("upgradePoints", 0);
         experience = PlayerPrefs.GetInt("xp", 0);
         experienceToNextLevel = PlayerPrefs.GetInt("xpToNextLvl", 30);
         playerSkills.SetSkillPoint(upgradePoints);
 
 
     }
 
 
 
     public void OnApplicationQuit()
     {
         PlayerPrefs.DeleteAll();
     }

I know this code is pretty ugly but hey, it works. However when OnApplicationQuit gets called, the values for all of my PlayerPrefs don't change back to their default values I have set. Any help or different ways to save this data would be greatly appreciated!

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
1

Answer by rh_galaxy · Apr 03 at 03:23 PM

How do you detect that the values are not the default? Are there other places in the code where you use PlayerPrefs?

One thing that might be missing in OnDisable() is to Save after the last Set, but if you want the values to disappear on quit maybe you don't need to.

 PlayerPrefs.Save(); //this saves all changes to disk

The doc doesn't say that you must do this after PlayerPrefs.DeleteAll(), but it might be worth trying.

Update: I did a small test and stepped through with the debugger. All worked as expected.

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 LiamBradley · Apr 04 at 04:43 PM 0
Share

Thanks, I'll try this when I get home.

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

139 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 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

PlayerPrefs - Can I save more than one? 1 Answer

How to set a numerical getint() and display it... 1 Answer

How can I use my saved playerprefs the next time the game gets started? 3 Answers

PlayerPrefs not saving slider data 3 Answers

PlayerPrefsX delete all values? 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