My shop system script is not working

Hi, I am trying to make a shop system but my script is not working. when I press buy button it doesn’t deduct the cost from the overall score also I want display a message if player don’t have enough points and the buy button changes to equip button after player bought the gameobject and a way to save the purchase in playerprefs or txt file and how will I change the gameobject that player equiped in the game scene please explain these all here is the script:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro;
using UnityEngine.EventSystems;

public class shop : MonoBehaviour
{
public TMPro.TextMeshProUGUI scoreText;

  public GameObject Item1;
  public GameObject Item2;
  public GameObject Item3;
  public GameObject Item4;

 private Dictionary<GameObject, float> ItemPrices;


void Start ()
{
    scoreText.text = "Score : " + ((int)PlayerPrefs.GetFloat ("Highscore")).ToString();

  ItemPrices = new Dictionary<GameObject, float>()
  {
   { Item1, 100f },
   { Item2, 2500f },
   {Item3, 3500f},
   { Item4, 5000f },
   };
}

public void PurchaseItem(GameObject Item)

{
foreach(KeyValuePair<GameObject, float> item in ItemPrices)
{
if (item.Key == Item)
{
// Take away the cost of the item from the player’s currency
float score = PlayerPrefs.GetFloat (“Highscore”);
score -= item.Value;
}
}
}
}

thanks

I believe this is because you obtain your score value, change the score value BUT never set the score value back again.

You might be thinking of regular classes where, if you change a value within them then that value remains changed but PlayerPrefs is more like a database of values than a class.

So to fix this code, all you need to do is add:

PlayerPrefs.SetFloat("Highscore", score);

Right after you deduct the item value from your obtained score.

Also, two other unrelated improvements:

Since the item you are passing to buy is the same object as the item key inside your dictionary, you can just obtain the value like so:

float itemValue = ItemPrices[Item];

As your item dictionary increases in size, this will be a lot more performant and easy to read given it’s how dictionaries are expected to be used. Although it’s probably worth having a check to see whether the key is present.

if (!ItemPrices.ContainsKey(Item))
{
    throw new Exception($"There is no key for gameobject '{Item}' in the ItemPrices dictionary");
}

Second note is that when using string keys like “Highscore” in your player prefs called, I’d use a const string just to avoid typos. Your sanity will thank me later!

// at top of class
public const string PlayerHighscoreKey = "Highscore";

// then use this instead of "Highscore" string literal
PlayerPrefs.GetFloat(PlayerHighscoreKey);

Even just typing that out I capitalised the S in score! This guarantees that whatever the key, all your calls use the same value.

@jackmw94 can you give the final product that how my code should look like and please also add those things that I mentioned in my question like how to save the purchase in playerprefs or txtfile and how to make a equip button that changes the gameObject in the main game scene