I am trying to make a shop system in unity where you can buy PCs using a currency called “Math”.
There are a total of two scenes, one is the main game scene and the other one is the shop scene. So there are these two arrows that when you press them, they show the next or previous PC in the shop just like most idle games do. I am trying to make all the variables like bool
and int
into PlayerPrefs
as much as possible so that the player can remain his or her status even if the game have been closed. But after I turned the PC.isUnlocked
bool
into a PlayerPrefs
, a bug appeared which made the game impossible to play. So basically, when I bought the previous PC, the next PC unlocked without even buying it I don’t know if it really unlocked or not but in the game scene, the next PC which unlocked itself without buying didn’t show up, instead, it was the PC that I bought. But that would cause a problem. The next PC’s buy button would disappear since that is what unlocked PC does, making it impossible to be purchased. You can refer to the image I uploaded. By the way, there is only two PCs in the game for now.
And here is the full script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro;
public class upgradeShopManager : MonoBehaviour
{
public int currentPCIndex;
public GameObject[ ] PCmodels;
public PCBlueprint[] PCs;
public Button buyButton;
// Start is called before the first frame update
void Update()
{
UpdateUI();
}
void Start()
{
currentPCIndex = PlayerPrefs.GetInt("SelectedPC", 0);
foreach (GameObject PC in PCmodels)
{
PC.SetActive(false);
}
PCmodels[currentPCIndex].SetActive(true);
}
public void ChangeNext()
{
PCmodels[currentPCIndex].SetActive(false);
currentPCIndex++;
if (currentPCIndex == PCmodels.Length)
currentPCIndex = 0;
PCmodels[currentPCIndex].SetActive(true);
PCBlueprint PC = PCs[currentPCIndex];
if (!PC.isUnlocked)
return;
PlayerPrefs.SetInt("SelectedPC", currentPCIndex);
}
public void ChangePrevious()
{
PCmodels[currentPCIndex].SetActive(false);
currentPCIndex--;
if (currentPCIndex == -1)
currentPCIndex = PCmodels.Length -1;
PCmodels[currentPCIndex].SetActive(true);
PCBlueprint PC = PCs[currentPCIndex];
if (!PC.isUnlocked)
return;
PlayerPrefs.SetInt("SelectedPC", currentPCIndex);
}
public void UnlockPC()
{
int boolToInt(bool val)
{
if (val)
return 1;
else
return 0;
}
PCBlueprint PC = PCs[currentPCIndex];
PlayerPrefs.SetInt(PC.name, 1);
PlayerPrefs.SetInt("SelectedPC", currentPCIndex);
PC.isUnlocked = true;
if (PC.isUnlocked = true)
{
PlayerPrefs.SetInt("isUnlocked", boolToInt(PC.isUnlocked));
}
PlayerPrefs.SetInt("math", PlayerPrefs.GetInt("math", 0) - PC.price);
}
private void UpdateUI()
{
bool intToBool(int val)
{
if (val != 0)
return true;
else
return false;
}
PCBlueprint PC = PCs[currentPCIndex];
if (PC.isUnlocked = intToBool(PlayerPrefs.GetInt("isUnlocked", 0)))
{
buyButton.gameObject.SetActive(false);
}
else
{
buyButton.gameObject.SetActive(true);
buyButton.GetComponentInChildren<TextMeshProUGUI>().text = "Buy-" + PC.price;
if (PC.price < PlayerPrefs.GetInt("math", 0))
{
buyButton.interactable = true;
}
else
{
buyButton.interactable = false;
}
}
}
}