Day 125 — Purchasing an Item From the Shop

public int selectedItem;
public int itemCost;
public void SelectItem(int item)
{
//0 = flame sword
//1 = flight boots
//2 = castle key
switch(item)
{
case 0:
UIManager.Instance.UpdateShopSelection(58);
selectedItem = 0;
itemCost = 200;
break;
case 1:
UIManager.Instance.UpdateShopSelection(-54);
selectedItem = 1;
itemCost = 400;

break;
case 2:
UIManager.Instance.UpdateShopSelection(-162);
selectedItem = 2;
itemCost = 100;
break;
default:
Debug.Log("Please assign an ID between 0-2");
break;
}
}
private Player _player;public void BuyItem()
{
if (_player.Diamonds >= itemCost)
{
_player.Diamonds -= itemCost;
Debug.Log("You've purchased: " + selectedItem);
Debug.Log("Gems remaining: " + _player.Diamonds);
_shopUI.SetActive(false);
}
else
{
Debug.Log("You have not got enough gems");
_shopUI.SetActive(false);
}
}
private static GameManager _instance;
public static GameManager Instance
{
get
{
if (_instance == null)
{
Debug.LogError("GameManager is null");
}
return _instance;
}
}
public bool HasKey { get; set; }private void Awake()
{
_instance = this;
}
public void BuyItem()
{
if (_player.Diamonds >= itemCost)
{
if (selectedItem == 2)
{
GameManager.Instance.HasKey = true;
}
_player.Diamonds -= itemCost;
Debug.Log("You've purchased: " + selectedItem);
Debug.Log("Gems remaining: " + _player.Diamonds);
_shopUI.SetActive(false);
}
else
{
Debug.Log("You have not got enough gems");
_shopUI.SetActive(false);
}
}

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store