Connecting Character Shop to Currency

Spent the past few days trying to come up with a way to connect my character selection to a currency/score manager but yielding no results.

Below is my character selection script. This works fine and the player is able to toggle between all the possible options and saves a the selected character. But would someone be able to help me out as to how I link in a currency script and enable/ disable to buy button depending on whether the player has enough money.

Also my select button is always active so the player need not buy the character. I tried

if (!menuCharacters[index].unlocked)
{
playButton.SetActive (true);
} else {
playButton.SetActive (false);
PlayerPrefs.SetInt (“PlayerSelected”, index);
SceneManager.LoadScene (“Menu”);
}

But this didn’t work. Any ideas?

using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;

public class CharacterSelectionScript : MonoBehaviour {

    [System.Serializable]
    public class MenuPlayer
    {
        [Header("Player Details")]
        public string name;
        public int price;
        public bool unlocked;
    }

    public static CharacterSelectionScript instance;
  


    public GameObject[] characterList;
    public int index;

    public GameObject buyButton, playButton;


    [Header("Player selection")]
    public MenuPlayer[] menuCharacters;

    void Awake()
    {
        MakeSingleton();
    }

    void MakeSingleton()
    {

        if (instance != null)
        {
            Destroy(gameObject);
        }
        else
        {
            instance = this;
            DontDestroyOnLoad(gameObject);
        }
    }




    void Start ()
    {

        index = PlayerPrefs.GetInt("PlayerSelected");

        characterList = new GameObject[transform.childCount];

        for (int i = 0; i < transform.childCount; i++) {
            characterList[i] = transform.GetChild (i).gameObject;
        }

        foreach (GameObject go in characterList) {
            go.SetActive (false);
        }
        if (characterList[index]) {
            characterList[index].SetActive (true);
        }

        //Fill the Characters ();
        CheckForUnlockedCharacters();
    }



    public void PrevPlayerButton()
    {
        characterList[index].SetActive (false);

        index--;
        if (index < 0) {
            index = characterList.Length - 1;
        }

        characterList[index].SetActive (true);

        //BUY BUTTON ACTIVATE
        if (!menuCharacters[index].unlocked)
        {
            buyButton.SetActive (true);
        } else {
            buyButton.SetActive (false);
        }
    }

    public void NextPlayerButton()
    {
        characterList[index].SetActive (false);

        index++;
        if (index == characterList.Length) {
            index = 0;
        }

        characterList[index].SetActive (true);

        //BUY BUTTON ACTIVATE
        if (!menuCharacters[index].unlocked)
        {
            buyButton.SetActive (true);
        } else {
            buyButton.SetActive (false);
        }

    }

    public void LoadGameScene()
    {
        PlayerPrefs.SetInt ("PlayerSelected", index);
        SceneManager.LoadScene ("Menu");
    }

    private void CheckForUnlockedCharacters()
    {
        //Check for unlokced players
        for (int i = 0; i < menuCharacters.Length; i++)
        {
            //First check if the player is pre-unlocked
            if (menuCharacters[i].unlocked)
            {
                PlayerPrefs.SetInt(menuCharacters[i].name, 1);
            }

            if (PlayerPrefs.GetInt(menuCharacters[i].name) == 1)
            {
                menuCharacters[i].unlocked = true;
            }
            else
            {
                menuCharacters[i].unlocked = false;
            }
        }
    }

    public void BuyButton()
    {
      
        {
            menuCharacters[index].unlocked = true;
            PlayerPrefs.SetInt(menuCharacters[index].name, 1);
            buyButton.SetActive (false);
            print ("Character Bought"); // if (money > character price) enable buy button
        }
      

    }
}

So, in the code where it checks for the ‘unlocked’, compare its price with your current “money value”. I would do this before clicking the buy button if you want to hide it (disable it visually/greyed out).

It’s like you almost answered your own question :slight_smile:

How you connect it I don’t know… is it another script that’s available here in this scene? Is it in playerprefs?