In Game Shop
Hello guys what's up i was working in game unity shop and i thought you guys can help me with something,have you ever play color switch or any Ketchapp game? if yes than you saw that in almost everygame there is a shop and i want to make a shop in that way here is my script...yes my scrip its working but i have a problem. when i buy an item it equipts it but i want my money to be -100 and and then to save that i bought that skin can someone help ? :) thank you all
public class Shopmenager : MonoBehaviour { private static int playerMoney; private Text MoneyHud; #region ItemConfig public Image itemIcon; public Text itemCost; public Text itemName; public int buttonIndex; public string ItemName; public string ItemCost; public Sprite ItemIcon; public Sprite BallIcon; public Sprite CointIcon; public Sprite bgWhite; public Sprite noBg; private Image backgroundImg; #endregion
string skinName = string.Empty;
private GameObject[] items;
void Start()
{
playerMoney = PlayerPrefs.GetInt("Coints");
skinName = PlayerPrefs.GetString("PlayerSprite");
backgroundImg = GetComponent<Image>();
items = GameObject.FindGameObjectsWithTag("item");
MoneyHud = GameObject.FindGameObjectWithTag("HudCoins").GetComponent<Text>();
itemName.text = ItemName;
itemCost.text = ItemCost;
itemIcon.sprite = ItemIcon;
}
void Update()
{
Debug.Log(MoneyHud);
print(skinName + " " + ItemName);
if (backgroundImg.sprite.name == bgWhite.name && skinName != ItemName)
{
print("Changing "+ItemName);
backgroundImg.sprite = noBg;
}
}
public void On_Click_Button()
{
int skin_buyed = 0;
playerMoney = PlayerPrefs.GetInt("Coints");
int val = playerMoney - Convert.ToInt32(ItemCost);
print(playerMoney + " - " + ItemCost + " = " + val);
if (val >= 0)
{
if (canBuy())
{
playerMoney = val;
if (playerMoney <= 0)
{
playerMoney = 0;
}
MoneyHud.text = playerMoney.ToString();
}
else
{
print("You already have this item.");
}
switch (buttonIndex)
{
case 1:
PlayerPrefs.SetString("PlayerSprite", null);
PlayerPrefs.SetString("EnemySprite", "Point");
break;
case 2:
skin_buyed = PlayerPrefs.GetInt("Skin2");
skin_buyed = PlayerPrefs.GetInt("Point2");
if (skin_buyed == 1)
{
PlayerPrefs.SetString("PlayerSprite", "Skin3");
PlayerPrefs.SetInt("Skin2", 1);
itemIcon.sprite = CointIcon;
PlayerPrefs.SetString("EnemySprite", "Point3");
PlayerPrefs.SetInt("Point2", 1);
}
else
{
print("You already buyed this item.");
}
break;
case 3:
skin_buyed = PlayerPrefs.GetInt("Skin3");
skin_buyed = PlayerPrefs.GetInt("Point3");
if (skin_buyed == 1)
{
PlayerPrefs.SetString("PlayerSprite", "Skin3");
PlayerPrefs.SetInt("Skin3", 1);
itemIcon.sprite = CointIcon;
PlayerPrefs.SetString("EnemySprite", "Point3");
PlayerPrefs.SetInt("Point3", 1);
}
else
{
print("You already buyed this item.");
}
default:
break;
}
skinName = PlayerPrefs.GetString("PlayerSprite");
print(skinName);
foreach(GameObject i in items)
{
Image img = i.GetComponent<Image>();
img.sprite = noBg;
}
if (skinName == ItemName)
{
backgroundImg.sprite = bgWhite;
itemIcon.sprite = BallIcon;
itemName.text = string.Empty;
itemCost.text = string.Empty;
}
}
else
{
print("You dont have enough coins " + ItemCost);
}
PlayerPrefs.SetInt("Coints", playerMoney);
}
public bool canBuy()
{
bool ok = true;
int skin_buyed = 0;
switch (buttonIndex)
{
case 1:
break;
case 2:
skin_buyed = PlayerPrefs.GetInt("Skin2");
if (skin_buyed == 1)
{
ok = false;
}
break;
case 3:
skin_buyed = PlayerPrefs.GetInt("Skin3");
if (skin_buyed == 1)
{
ok = false;
}
break;
default:
break;
}
return ok;
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612124452im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Guiskin custom 0 Answers
error in store,store unity app 0 Answers
How to Save the Current that what load from playerprefs 0 Answers
Skin Morph Problem 2 Answers