Troubles in making a shop system in unity

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;
            }
        }
    }
}

It’s a very simple mistake:

if (PC.isUnlocked = true)

That’s setting PC.isUnlocked to true! You want to check if it’s unlocked:

if (PC.isUnlocked == true)

Or, much simpler,

if (PC.isUnlocked)

In C#, assignment (=) returns the value assigned. So you can do eg. a = b = c = 3, which would set all of those values to 3. The downside to that is that you can do a spelling mistake like you did, and write if (a = true), which both sets a to true and checks the value of a. That’s pretty much always a mistake.

You have done the same thing in a couple of spots in the code, and that’s why things are showing up as already unlocked.

Make sure that your playerprefs are setup properly. ie they are reseting properly. also what is “PC Blueprint”

here are a couple of issues in your script i solve the issues of the your 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;

    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);

        do
        {
            currentPCIndex++;
            if (currentPCIndex == PCmodels.Length)
                currentPCIndex = 0;
        } while (!PCs[currentPCIndex].isUnlocked);

        PCmodels[currentPCIndex].SetActive(true);
        PCBlueprint PC = PCs[currentPCIndex];
        if (!PC.isUnlocked)
            return;

        PlayerPrefs.SetInt("SelectedPC", currentPCIndex);
    }

    public void ChangePrevious()
    {
        PCmodels[currentPCIndex].SetActive(false);

        do
        {
            currentPCIndex--;
            if (currentPCIndex == -1)
                currentPCIndex = PCmodels.Length - 1;
        } while (!PCs[currentPCIndex].isUnlocked);

        PCmodels[currentPCIndex].SetActive(true);
        PCBlueprint PC = PCs[currentPCIndex];
        if (!PC.isUnlocked)
            return;

        PlayerPrefs.SetInt("SelectedPC", currentPCIndex);
    }

    public void UnlockPC()
    {
        int boolToInt(bool val)
        {
            return val ? 1 : 0;
        }

        PCBlueprint PC = PCs[currentPCIndex];
        PlayerPrefs.SetInt(PC.name, 1);
        PlayerPrefs.SetInt("SelectedPC", currentPCIndex);
        PC.isUnlocked = true;
        if (PC.isUnlocked)
        {
            PlayerPrefs.SetInt("isUnlocked", boolToInt(PC.isUnlocked));
        }
        PlayerPrefs.SetInt("math", PlayerPrefs.GetInt("math", 0) - PC.price);
    }

    private void UpdateUI()
    {
        bool intToBool(int val)
        {
            return val != 0;
        }

        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;
            }
        }
    }
}