Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

How to delete all Player Prefs on the Android device!!!

Discussion in 'Scripting' started by StarShipMan, Oct 28, 2014.

  1. StarShipMan

    StarShipMan

    Joined:
    Jan 28, 2014
    Posts:
    91
    Hey everyone,

    I have my game running on a android device and im using player prefs to save which ever level the player has completed (silver trophy will be unlocked). The levels also has a trophies system set up depending on which objective has been completed.

    My problem is i have created a button to delete all of the player prefs from the game i.e. lock all of the unlocked levels again for testing purposes however i cannot get the game to delete everything i.e. gold trophy data gets deleted however the levels are still unlocked when they should be locked.

    Is there any other way to delete all of the saved data (player prefs) on the android device????

    This scipt is attached to a button and it works as it should in the editor however it does not work properly on the device.
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class PlayerSavedDataDelete : MonoBehaviour
    5. {
    6.  
    7.     void Start ()
    8.     {
    9.         PlayerPrefs.DeleteAll ();
    10.     }
    11.  
    12. }
     
  2. GarthSmith

    GarthSmith

    Joined:
    Apr 26, 2012
    Posts:
    1,240
  3. DoomSamurai

    DoomSamurai

    Joined:
    Oct 10, 2012
    Posts:
    159
    Unless I'm missing something, PlayerPrefs.DeleteAll should work. It's what it's there for. If it doesn't, I think you need to report this as a bug.
     
  4. cmcpasserby

    cmcpasserby

    Joined:
    Jul 18, 2014
    Posts:
    315
    PlayerPrefs.DeleteAll() works fine I used it in my last 2 games which target android 4.x and it works every time.
     
  5. axenlader

    axenlader

    Joined:
    Feb 18, 2014
    Posts:
    6
    Hi,
    This problem seems to be frequent on some Android devices.
    Anyway, you can remove each saved datas with PlayerPrefs.DeleteKey
    If you don't know the name of every key you use, you can override functions of PlayerPrefs in order to store the keys list and then do something like this :
    for (var key in storedKeys) {
    PlayerPrefs.DeleteKey(key);​
    }
     
  6. StarShipMan

    StarShipMan

    Joined:
    Jan 28, 2014
    Posts:
    91
    I will research more on the link that you have provided. Thanks
     
  7. StarShipMan

    StarShipMan

    Joined:
    Jan 28, 2014
    Posts:
    91
    I hope you are right, it is really annoying as i cant reset my only testing device. I might end up uploading a game that the levels stop working half way through, even though they seem to be working fine in the editor.
     
  8. StarShipMan

    StarShipMan

    Joined:
    Jan 28, 2014
    Posts:
    91
    Hi, i have about 40 levels in my games which equates to many keys :( i will try to use your solution. Im somewhat relieved that it is kind of a common occurrence whats happening with my device.