Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Question Cannot get PlayerPrefs keys to delete on public void or on click rather.

Discussion in 'Scripting' started by justinmed, Apr 1, 2024.

  1. justinmed

    justinmed

    Joined:
    Dec 19, 2021
    Posts:
    106
    For some reason I am having a hard time deleting my PlayerPref keys with a public void for a button click function. So that when Main Menu is clicked in the game, the timer keys and score keys delete and then reset by deleting. I have tried it within the timer and score script and in a separate script which I will show. I thought it was as simple as PlayerPrefs.DeleteKey("keyName") or PlayerPrefs.DeleteAll() but that is not working for me.

    here is the part from the score script.
    Code (CSharp):
    1. public void ReturnToMenu()
    2.  
    3.     {
    4.         SceneManager.LoadScene(1);
    5.         PlayerPrefs.DeleteKey("scoreKey");
    6.     }
    Here is the exert from the timer script.
    Code (CSharp):
    1. public void DeleteTimeKey()
    2.     {
    3.         PlayerPrefs.DeleteKey("timeValue");
    4.     }

    and here is the delete all key script.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class DeleteAllPPkeys : MonoBehaviour
    6. {
    7.     public static void DeleteAllPPKeys()
    8.  
    9.     {
    10.         PlayerPrefs.DeleteAll();
    11.     }
    12.     public void DeleteAllKeys()
    13.     {
    14.         PlayerPrefs.DeleteAll();
    15.     }
    16. }
    17.  
    Any help is greatly appreciated. I have gotten the keys to delete on application quit, and you would think that it is just as simple as that but with a public void or static void in the case of a delete all key command. But, that is not working for me so far.
     
  2. justinmed

    justinmed

    Joined:
    Dec 19, 2021
    Posts:
    106
    I got it to work, instead of having it on the main menu button to get out of the scene I just put it on the button to get back into the scene and PlayerPrefs.DeleteAll() worked.