I’ve been following Flarvain’s tutorial on how to make a shop system in unity.
(1) Shop Tutorial Unity - [2021] - YouTube.
(1) Shop Tutorial Unity #2 [2021] - Scriptable Objects - YouTube
(1) Shop Tutorial Unity #3 [2021] - Handling Purchases - YouTube.
I am a little in over my head as you could tell, but I want to expand on the shop system by letting the player display the purchased item on the main screen once the purchase has been made. I’ve managed to get the button to behave in the way that I need, however I don’t understand how to save the changes.
I’ve used PlayerPrefs in a separate script to save the currency with which the player has to buy their items. But that doesn’t seem to work here.
Any and all help apprecitated:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro;
public class WardrobeManager : MonoBehaviour
{
[Header("Panel")]
public GameObject wardrobePanel;
public GameObject[] wardrobePanelTwo;
public WardrobeTemplate[] wardrobeTemplate;
public CollectableItem[] collectableItem;
public Button[] buyButton;
public Button[] ondisplayButton;
public GameObject[] buyHolder;
public GameObject[] ondisplayHolder;
public Image[] displayedItem;
private CoinsManager coinsManager;
private string wardrobekey = "Put On Display";
//bool buybuttonClicked = false;
// Start is called before the first frame update
void Start()
{
coinsManager = FindObjectOfType<CoinsManager>();
for (int i = 0; i < collectableItem.Length; i++)
{
wardrobePanelTwo[i].SetActive(true);
ondisplayHolder[i].SetActive(false);
}
LoadPanel();
}
public void OpenPanel()
{
wardrobePanel.SetActive(true);
}
public void ClosePanel()
{
wardrobePanel.SetActive(false);
}
// Update is called once per frame
void Update()
{
CheckPurchaseable();
}
public void CheckPurchaseable()
{
for (int i = 0; i < collectableItem.Length; i++)
{
if (coinsManager.coins >= collectableItem[i].price)
{
buyHolder[i].SetActive(true);
buyButton[i].interactable = true;
}
else
{
buyHolder[i].SetActive(true);
buyButton[i].interactable = false;
}
}
}
public void PurchaseCollectable(int btnNo)
{
if (coinsManager.coins >= collectableItem[btnNo].price)
{
coinsManager.coins = coinsManager.coins - collectableItem[btnNo].price;
coinsManager.coinsUI.text = coinsManager.coins.ToString();
buyHolder[btnNo].SetActive(false);
CheckPurchaseable();
}
ondisplayHolder[btnNo].SetActive(true);
}
public void LoadPanel()
{
for (int i = 0; i < collectableItem.Length; i++)
{
wardrobeTemplate[i].titleTxt.text = collectableItem[i].title;
wardrobeTemplate[i].priceTxt.text = collectableItem[i].price.ToString();
}
}
}