- Home /
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 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
Answer by jackmw94 · Jan 16, 2021 at 12:27 PM
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.
Answer by Ashmit2020 · Jan 16, 2021 at 12:37 PM
@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
Are you asking this because you don't understand something that I've said or because you want to copy and paste something in and forget about it?
If it's the former I am more than happy to help, please let me know which bits of my reply you're struggling on and I'll go into it further. I appreciate you might want to learn from an example but I think if I talk you through how to fix it yourself you're more likely to learn from it :) I'll post a comment on my original post about the additional bits of functionality!
But if it's the latter then the answer is no - I'm happy to spend my time helping people learn C# and Unity but would rather not spend it writing source code for peoples' personal projects that I won't get paid for, won't ever be published and will almost certainly be passed off as their own. If you were looking for a copy and pasteable answer instead of insight then ask yourself why you want to build this game.
Your answer
Follow this Question
Related Questions
C# how to save game when player picks up an object? 3 Answers
Distribute terrain in zones 3 Answers
Multiple Cars not working 1 Answer
shop script not working 1 Answer