- Home /
how to make a shop system
Hi I am having a problem and that problem is pretty straight I don't know how to make a shop system and I need a shop system for my game I watched a lot of tutorials but nothing is working for me. These are the scripts that will be used in the shop system.
these are the scripts that store score
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using TMPro; using System;
public class PlayerHealth : MonoBehaviour {
public GameObject death;
public int maxHealth = 100;
public static int currentHealth;
public HealthBar healthBar;
public GameObject gameOverPanel;
// Start is called before the first frame update
void Start()
{
currentHealth = maxHealth;
healthBar.SetMaxHealth(maxHealth);
}
// Update is called once per frame
void Update()
{
}
public void TakeDamage(int damage)
{
currentHealth -= damage;
healthBar.SetHealth(currentHealth);
if( currentHealth <= 0)
{
Die();
}
}
void Die ()
{
Destroy(gameObject);
audioman.PlaySound ("explosion");
Instantiate(death, transform.position, Quaternion.identity);
PlayerPrefs.SetFloat ("Highscore", PlayerPrefs.GetFloat("Highscore", 0) + ScoreScript.scoreValue);
gameOverPanel.SetActive(true);
ScoreScript.scoreValue = 0;
//script.Score.text = (ScoreScript.scoreValue).ToString();
//Time.timeScale = 0f;
}
}
and this is the script that displays saved score in shop menu ui
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 int[,] shopItems = new int[5,5]; //public float Highscore; //public TMPro.TextMeshProUGUI scoreText;
void Start () { scoreText.text = "Score : " + ((int)PlayerPrefs.GetFloat ("Highscore")).ToString();
//Items;
/* shopItems[1, 1] = 1;
shopItems[1, 2] = 2;
shopItems[1, 3] = 3;
shopItems[1, 4] = 4;
//Price
shopItems[2, 1] = 100;
shopItems[2, 2] = 2500;
shopItems[2, 3] = 3500;
shopItems[2, 4] = 5000;
//Quantity
shopItems[3, 1] = 0;
shopItems[3, 2] = 0;
shopItems[3, 3] = 0;
shopItems[3, 4] = 0;*/
}
/* public void Buy() { GameObject ButtonRef = GameObject.FindGameObjectWithTag("Event").GetComponent().currentSelectedGameObject;
if (Highscore >= shopItems[2, ButtonRef.GetComponent<GRIM>().ItemID])
{
Highscore -= shopItems[2, ButtonRef.GetComponent<GRIM>().ItemID];
scoreText.text = "Score : " + ((int)PlayerPrefs.GetFloat ("Highscore")).ToString();
}
}*/ } These are the scripts I think will be needed in the shop system. Ok so I have 4 fighter jets that the player can buy. I want these things to be happen when player buy the jet the cost will deduct from the overall score and if the player don't have enough points it shows a error. But if the player has enough points the player gets the jet and buy button changes to equip button and the most important thing when the player clicks equip after buying the jet plane prefab he/she is using changes to the prefab they equipped. I think this is all the basics of shop system. But how can I do that? Please provide me the code for it and sorry for asking too much this my first time making a shop system and yes the fighter jets are not modifiers they are separate game object prefabs like vehicles. Thankyou
Thankyou
Answer by Llama_w_2Ls · Jan 15, 2021 at 11:30 AM
Item Prices
First of all, you might want to have a dictionary of a list of items, alongside their price. For example:
public Dictionary<GameObject, float> ItemPrices = new Dictionary<GameObject, float>();
You would need using System.Collections.Generic;
at the top of your script to access the dictionary class.
Then, you could fill in the values in the inspector of each item (gameobject) and their price (float). Alternatively, you can make the dictionary private, and change the values in the script:
Dictionary<GameObject, float> ItemPrices = new Dictionary<GameObject, float>()
{
{ Item1, 10f },
{ Item2, 7.50f },
{ Item3, 12.75 },
};
// etc...
Purchasing Items
If an item is purchased, you would need to search through the dictionary and look for the item you purchased, then minus the cost from the player's currency. You might want to check if they can buy the item, before they purchase it:
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
playerCurrency -= item.Value;
}
}
}
Saving Items
Finally, you could save a text file that contains any items the player has purchased. Or you could use playerPrefs to save the player's current currency remaining. @Ashmit2020
@Llama_w_2Ls I did as you said to do this is the script after:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using T$$anonymous$$Pro;
using UnityEngine.EventSystems;
public class shop : $$anonymous$$onoBehaviour
{
public T$$anonymous$$Pro.Text$$anonymous$$eshProUGUI scoreText;
public Dictionary<GameObject, float> ItemPrices = new Dictionary<GameObject, float>();
public GameObject Item1;
public GameObject Item2;
public GameObject Item3;
public GameObject Item4;
void Start ()
{
scoreText.text = "Score : " + ((int)PlayerPrefs.GetFloat ("Highscore")).ToString();
Dictionary<GameObject, float> ItemPrices = new Dictionary<GameObject, float>()
{
{ Item1, 1000f },
{ Item2, 2500f },
{Item3, 3500f},
{ Item4, 5000f },
};
}
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
PlayerPrefs.GetFloat ("Highscore")) -= item.Value;
}
}
}
}
Can you please check it again and how will the script figure out which item is the player buying I have four items and four different buy buttons how are the buttons and script supposed suppose to figure that out please explain that. I am also having another problem at line 76 that is (PlayerPrefs.GetFloat ("Highscore")) -= item.Value;)
I am storing the points that the player earned in a playerpref but when I command $$anonymous$$us from playerpref like this "Highscore" -= item.Value;
or PlayerPrefs.GetFloat ("Highscore")) -= item.Value;
it shows errors can you please tell me how can I fix that. ThankYou
If you make the method PurchaseItem()
public, and assign it to each of your 4 buttons, a slot in the OnClick area of the button should appear, which takes in a GameObject. This corresponds to the item you're purchasing. $$anonymous$$g. pressing button1 runs the method PurchaseItem
, with Item1 as the parameter.
About the PlayerPrefs issue, what error are you getting? Subtracting a float from another float is a possible operation and there shouldn't be any error. Have you tried:
PlayerPrefs.GetFloat("Health") -= item.Value;
It seems as if you have an extra bracket in there.
Finally, you didn't create the dictionary correctly. You don't need this line: public Dictionary<GameObject, float> ItemPrices = new Dictionary<GameObject, float>();
if you're filling the dictionary up in the script, which you are doing. Secondly, the function PurchaseItem()
cannot access the Dictionary, as you declared it in the Start method. Your script should look like this:
public class shop : $$anonymous$$onoBehaviour
{
public T$$anonymous$$Pro.Text$$anonymous$$eshProUGUI 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, 1000f },
{ 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
PlayerPrefs.GetFloat ("Highscore") -= item.Value;
}
}
}
}
@Llama_w_2Ls I am getting this issue about playerprefs: Assets\scripts\shop.cs(41,18): error CS0131: The left-hand side of an assignment must be a variable, property or indexer even when I am doing this PlayerPrefs.GetFloat ("Highscore") -= item.Value; and how will I save what the player bought in a playerpref and how can I make buy button change to equip button after player successfully bought the GameObject. Then how can I make the equip button change the vehicle(GameObject) in the main game scene cuz the shop menu and the main game are in different scenes?
Sorry, my bad. It should be:
float score = PlayerPrefs.GetFloat("Highscore");
score -= item.Value;
To save what the player bought, you might want to use something a little different to PlayerPrefs. You could save an individual string for each item into playerPrefs, but it would be easier to write to a text file, containing the contents of what you bought, then reading it back again, when loading your inventory.
You're asking a lot of questions, and it would be easier to answer individual questions. $$anonymous$$aybe you could post another question?
Your answer
Follow this Question
Related Questions
points system gets overrwriten 3 Answers
Image as boundary 1 Answer
gameobject not setting active after timer ends 2 Answers
Screen scaling causing text to be outside button 0 Answers
Unity Android Screen Glitch 1 Answer