Trouble with reseting playerprefs

Ive been trying and trying for a month now to get this script to work so I really need help. This script is supposed to make it so that when the player clicks on an object and if the players coin count > = to the price of the object and it has not been purchased yet then it will then be purchased. I tried to make it so that it saves it as purchased in playerprefs. But if the player presses the space button then it resets it to not purchased so I can test the game. But once I start the game and if I only purchase one button and then go to my title screen and back to he shop scene they are all = purchased. Does anyone know why? And yes, I did rename space to MySpace.

using System.Collections;

public class RedButton : MonoBehaviour {
    public int pointsToAdd;
    public int isPurchased;
    public int pointsToSubtract;
    public Texture textureToChangeTo;
    int zero;

    void Start () {
   
            isPurchased = PlayerPrefs.GetInt ("purchase");
       
        ScoreManager.score = PlayerPrefs.GetInt ("CurrentScore");
        zero = 0;
    }
   
   
    public void OnMouseDown()
    {
        if (isPurchased == 0 && ScoreManager.score >= pointsToSubtract) {
            ScoreManager.score -= pointsToSubtract;
       
            PlayerPrefs.SetInt ("CurrentScore", ScoreManager.score);

            PlayerPrefs.SetInt ("purchase", 1);
            //isPurchased = PlayerPrefs.GetInt ("purchase");
            Debug.Log ("You purchased a thingy");
       
       
        } else if (isPurchased == 1) {
            Debug.Log ("You have already purchased this thing");
            ScoreManager.score += zero;
        } else if (PlayerPrefs.GetInt ("purchase") == 0 && ScoreManager.score < pointsToSubtract) {
            Debug.Log ("not enough coins");
        }





    }

    void Update()
    {
        if (Input.GetButtonDown ("MySpace"))

            PlayerPrefs.DeleteAll ();
        Debug.Log ("Supposedly deleted playerprefs");
       
   


    }



}

Try forcing a save after you delete.

PlayerPrefs.DeleteAll();
PlayerPrefs.Save();

Also, I don’t think it’s realizing when I’m hitting the space bar. Because randomly it starts sending a message to the console and it’s just spamming the console window.

Since you are having a problem with the space bar, try hard coding it for now.

if(Input.GetKeyDown(GetKey.Space))
{
}

I can’t test your code at the moment, so I’m guessing that “MySpace” is a user custom button. Once you confirm that the space bar is not the issue, then you can focus on setting/getting the playerprefs.

After looking a bit more at your script and thinking about it, I am questioning how you are differentiating between different items. My guess is that you are using 1 playerprefs “purchase” for all of your purchases. If so that would be the reason why all of your items = purchased. For each item you would need a different playerpref; or you could do it with an array, where the array holds all items that have been purchased. But somewhere somehow you have to tell the playerprefs that only Item A, B & D were purchased, but not Item C.

My first paragraph while I believe it to be sound might be confusing. So let me try explaining in a different way. You may have this script on multiple objects (or UI buttons) which is fine. Each script will be handled independently for each button or object. However you only have 1 playerprefs reference that is used by all of the scripts that may be attached to objects. So when you are looking for if Item A was purchased it retrieves “purchase” from playerprefs. However playerprefs is updating all Items using the same “purchase” so all items will have the same result of being purchased. So when you are asking if Item A was purchased, it comes back with the result yes, but it also gives that same result for ALL of your items, because they are all tied to one playerprefs.

TLDR: Make sure that each of your items is referenced separately of the others in playerprefs, either through the use of Item01purchase, Item02purchase etc. or through an array/list.

Thank you, as soon as I get on my computer I will be sure to try that out. Thanks.