- Home /
How can I save my shop data
Hi, In my game I want to save the purchase that player made and load it every time when the player opens the shop and also don't get reset when the player exits the game. In the shop when a purchase went successfully the buy button changes to equip button and the remaining points get saved but the purchase doesn't get saved if a purchase went successfully and the buy button changes to equip button and if the player entered another scene or quit the game the buttons gets reset and the player has to buy it again so how can I fix that? I want the game to save the shop state. So pls tell me what should I do to the shop script to save it.
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; public TMPro.TextMeshProUGUI notenough; public TMPro.TextMeshProUGUI notenough1; public TMPro.TextMeshProUGUI notenough2; public TMPro.TextMeshProUGUI notenough3; public GameObject buyButton; public GameObject buyButton1; public GameObject buyButton2; public GameObject buyButton3; public GameObject equipButton; public GameObject equipButton1; public GameObject equipButton2; public GameObject equipButton3; public float sec = 14f;
private Dictionary ItemPrices;
void Start () { scoreText.text = "Score : " + ((int)PlayerPrefs.GetFloat ("Highscore")).ToString();
ItemPrices = new Dictionary() { { Item1, 50f }, { Item2, 80f }, {Item3, 3500f}, { Item4, 5000f }, }; notenough.enabled = false; notenough1.enabled = false; notenough2.enabled = false; notenough3.enabled = false;
}
public void PurchaseItem(GameObject Item) { foreach(KeyValuePair item in ItemPrices) { if (item.Key == Item) {
float price = item.Value;
float val = PlayerPrefs.GetFloat ("Highscore");
if(val >= price)
{
val -= item.Value;
PlayerPrefs.SetFloat("Highscore", val);
scoreText.text = "Score : " + ((int)PlayerPrefs.GetFloat ("Highscore")).ToString();
buyButton.SetActive(false);
equipButton.SetActive(true);
// Take away the cost of the item from the player's currency
//PlayerPrefs.GetFloat ("Highscore") -= item.Value;
}
else
{
Debug.Log("not enough money");
notenough.enabled = true;
notenough1.enabled = true;
notenough2.enabled = true;
notenough3.enabled = true;
Invoke("noen", 2);
}
}
}
}
public void PurchaseItem1(GameObject Item) { foreach(KeyValuePair item in ItemPrices) { if (item.Key == Item) {
float price = item.Value;
float val = PlayerPrefs.GetFloat ("Highscore");
if(val >= price)
{
val -= item.Value;
PlayerPrefs.SetFloat("Highscore", val);
scoreText.text = "Score : " + ((int)PlayerPrefs.GetFloat ("Highscore")).ToString();
buyButton1.SetActive(false);
equipButton1.SetActive(true);
// Take away the cost of the item from the player's currency
//PlayerPrefs.GetFloat ("Highscore") -= item.Value;
}
else
{
Debug.Log("not enough money");
notenough.enabled = true;
notenough1.enabled = true;
notenough2.enabled = true;
notenough3.enabled = true;
Invoke("noen", 2);
}
}
}
}
public void PurchaseItem2(GameObject Item) { foreach(KeyValuePair item in ItemPrices) { if (item.Key == Item) {
float price = item.Value;
float val = PlayerPrefs.GetFloat ("Highscore");
if(val >= price)
{
val -= item.Value;
PlayerPrefs.SetFloat("Highscore", val);
scoreText.text = "Score : " + ((int)PlayerPrefs.GetFloat ("Highscore")).ToString();
buyButton2.SetActive(false);
equipButton2.SetActive(true);
// Take away the cost of the item from the player's currency
//PlayerPrefs.GetFloat ("Highscore") -= item.Value;
}
else
{
Debug.Log("not enough money");
notenough.enabled = true;
notenough1.enabled = true;
notenough2.enabled = true;
notenough3.enabled = true;
Invoke("noen", 2);
}
}
}
}
public void PurchaseItem3(GameObject Item) { foreach(KeyValuePair item in ItemPrices) { if (item.Key == Item) {
float price = item.Value;
float val = PlayerPrefs.GetFloat ("Highscore");
if(val >= price)
{
val -= item.Value;
PlayerPrefs.SetFloat("Highscore", val);
scoreText.text = "Score : " + ((int)PlayerPrefs.GetFloat ("Highscore")).ToString();
buyButton3.SetActive(false);
equipButton3.SetActive(true);
// Take away the cost of the item from the player's currency
//PlayerPrefs.GetFloat ("Highscore") -= item.Value;
}
else
{
Debug.Log("not enough money");
notenough.enabled = true;
notenough1.enabled = true;
notenough2.enabled = true;
notenough3.enabled = true;
Invoke("noen", 2);
}
}
}
}
/*public void buy() {
if (val >= price) { PurchaseItem(); } else {
}*/
public void noen() { notenough.enabled = false; notenough1.enabled = false; notenough2.enabled = false; notenough3.enabled = false;
}
} Pls help Thanks
Answer by logicandchaos · Jan 22, 2021 at 02:42 PM
I think the best way to save anything is to put all the data in a scriptable object then save it to an external file, txt or bin. There are many tutorials on this online. Playerprefs is not a secure save location and can be altered by players.