I want to make a script for buying an item.
using UnityEngine;
using UnityEngine.UI;
public class Price : MonoBehaviour
{
public Text totalCoins;
public int price = 0;
public int skinID = 0;
public GameObject lockedOverlay;
public GameObject priceTag;
public Button buyButton;
public void Buy(int skinNumber)
{
if (MoneySystem.totalMoney >= price && Shop.oneOwned == 0)
{
buyButton.interactable = false;
MoneySystem.totalMoney = MoneySystem.totalMoney - price;
PlayerPrefs.SetInt("Coins", MoneySystem.totalMoney);
totalCoins.text = PlayerPrefs.GetInt("Coins").ToString();
lockedOverlay.SetActive(false);
priceTag.SetActive(false);
print("Item bought! Enjoy your purchase. Money remaining: " + MoneySystem.totalMoney);
}
}
The idea is that i put this script on every button i can press to buy. On the first button in the inspector i put 1, on the second i put 2 (at skinID) and etc.
Then i want to make it so when you click one of them, if you have enough money, and dont already own the skin, it buys a skin coresponding to the skinNumber ( dont know how to do this either, thats why you will see skinID and skin Number too, im confused and trying to work it out ) set to the button. Then you cant buy it again.
The way i blocked the ability to buy again is by setting button.interactable to false. But. There is the problem.
I didn’t test it yet because i didnt linked the menu to turn on and off to a button yet as im still working on it, but its obvious that if you restart the scene, the button.interactable would be set to true by default.
After you buy a skin basically i want to make it remember you own that skin and can equip it at any time.
How can i do that?
Also i am a complete noob so please explain accordingly ![]()
EDIT:
This is how im planning to do the part with buying.
But still how do i make it so it remembers if you own a skin? And ( i know it has to do with player prefs)
And how do i make it so if i click on a skin you have, you can equip it?
I know that will be a problem considering that i deactivate the button when you buy a skin…
So i need to replace that with something else
public void Buy(int skinID)
{
if (MoneySystem.totalMoney >= price)
{
buyButton.interactable = false;
MoneySystem.totalMoney = MoneySystem.totalMoney - price;
PlayerPrefs.SetInt("Coins", MoneySystem.totalMoney);
totalCoins.text = PlayerPrefs.GetInt("Coins").ToString();
lockedOverlay.SetActive(false);
priceTag.SetActive(false);
print("Item bought! Enjoy your purchase. Money remaining: " + MoneySystem.totalMoney);
}
if (skinID == 1)
{
print(skinID); // i think i need something with player prefs here.
}
if (skinID == 2)
{
print(skinID); // i think i need something with player prefs here.
}
}