• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
2
Question by Ashmit2020 · Jan 15 at 08:53 AM · uiunity 2dshopbuycurrency

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Llama_w_2Ls · Jan 15 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

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Ashmit2020 · Jan 15 at 03:21 PM 0
Share

@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 minus 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

avatar image Llama_w_2Ls Ashmit2020 · Jan 15 at 04:13 PM 0
Share

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. E.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;
            }     
       }
   }
  }

@Ashmit2020

avatar image Ashmit2020 · Jan 16 at 06:01 AM 0
Share

@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?

avatar image Llama_w_2Ls Ashmit2020 · Jan 16 at 10:44 AM 0
Share

Sorry, my bad. It should be:

 float score = PlayerPrefs.GetFloat("Highscore");
 score -= item.Value;

@Ashmit2020


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?

avatar image Ashmit2020 Llama_w_2Ls · Jan 16 at 11:11 AM 0
Share

Sry for asking too much I am a newbie

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

198 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

points system gets overrwriten 3 Answers

How do i prevent the player from dragging a ui element off screen? 1 Answer

OnPointerClick in child's script is not triggered when there exists a OnPointerDown in parent's script 1 Answer

How to restart game while keeping changes done? Alternative for Application.loadlevel. 1 Answer

I am trying to make a shop system but I am having this doubt 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges