Saving variables stored on a button using PlayerPref

I have a script that makes the user able to purchase items by clicking on a button. In that script I have variables such as Count (How many times the item has been purchased), cost (the cost of the item) and power (How much power the user receives when item purchased).
This script has been added to 4 buttons, which has different cost values, count values and power values assigned to them. Example:
Button 1 has a cost value of 100, count value of 1 and Power value of 50.
Button 2 has a cost value of 250, count value of 5 and Power value of 200.
etc, etc
These values has been assigned in the script component added to the button.

Here is my problem.
How do I use PlayerPref to save the individual values that has been assigned onto the button.

While I’m not sure if playerPrefs is your best use case, you’ll have a few different options. One is to convert your class to a json string and save that string out. Two is to just create your own string and then when you pull it, split it and assign the values. The json is usually better in my opinion as it’s simply pulling the string and using the json serializer to handle things.

However, there may be better options.

What are you trying to store in playerprefs exactly? The values for the buttons so you can create these buttons at run time? Or do you just need to store something when a player makes a purchase?

I’m currently working on an UI incremental game, where I want to run an autosave method created with Playerprefs, as that seems to be good enough for my use of variables and such that needs to be saved.

This is the code which all buttons has. The cost of the item will be assigned on the C# Script component added onto the button.

// Adds power to Per Click when the user purchases the item. Gives the item a new value when purchased.
    public void GoldPerClickPurchasedUpgrade()
    {
        if (ClickManager.gold >= cost)
        {
            ClickManager.gold -= cost;
            count += 1;
            clickManager.goldPerClick += clickPower;
            clickManager.goldPerClickText.text = "Gold Per Click: " + clickManager.goldPerClick;
            cost = Mathf.Round(baseCost * Mathf.Pow(1.15f, count));
        }
    }

The problem lays with how I can grab the value which the button has on it, such as cost. If I try to create a new instance of the script that has the purchase method in it and all the values, and if I then use PlayerPrefs to save the cost value, it will only save one value and then load that value onto all of the buttons. It doesn’t recognize that each button has a different value.

You’re going to have to use multiple playerprefs, one per button, or you have to do a json string which would hold a list of all button values (basically a list of a class with values).

So, if I have three buttons, I might have three playerprefs named button1, button2, button3.

If you don’t have a set number of buttons and it could change, the json method may be a better way to go.

I managed to make it work now. I did what I first tried and now I managed to make it work. It seems the first time I tried doing it this way, I forgot to make the string path addresses different by not adding a number (Example: “count1”).
This way is probably the least efficient way to do it, but it does the job.

// This will create a new instance of the upgradeManger script, so we can assign the button values to individual instances and save them individually.
    public UpgradeManager clickItem1;
    public UpgradeManager clickItem2;
    public UpgradeManager clickItem3;
    public UpgradeManager clickItem4;
    public UpgradeManager clickItem5;
    public UpgradeManager clickItem6;
    public UpgradeManager clickItem7;
    public UpgradeManager clickItem8;

    /// <summary>
    /// SAVE/LOAD KEYS
    /// </summary>

    // UpgradeManager Variables
    // Item 1 //
    private string count1 = "count1";
    private string cost1 = "cost1";
    private string clickPower1 = "clickPower1";
    // Item 2 //
    private string count2 = "count2";
    private string cost2 = "cost2";
    private string clickPower2 = "clickPower2";
    // Item 3 //
    private string count3 = "count3";
    private string cost3 = "cost3";
    private string clickPower3 = "clickPower3";
    // Item 4 //
    private string count4 = "count4";
    private string cost4 = "cost4";
    private string clickPower4 = "clickPower4";
    // Item 5 //
    private string count5 = "count5";
    private string cost5 = "cost5";
    private string clickPower5 = "clickPower5";



    /// <summary>
    /// Starting point
    /// </summary>

    // Saves all variables
    public void SaveGame()
    {
        // Save Variables from UpgradeManager Script
        // Item 1 //
        PlayerPrefs.SetInt(count1, clickItem1.count);
        PlayerPrefs.SetInt(clickPower1, clickItem1.clickPower);
        PlayerPrefs.SetFloat(cost1, clickItem1.cost);
        // Item 2 //
        PlayerPrefs.SetInt(count2, clickItem2.count);
        PlayerPrefs.SetInt(clickPower2, clickItem2.clickPower);
        PlayerPrefs.SetFloat(cost2, clickItem2.cost);
        // Item 3 //
        PlayerPrefs.SetInt(count3, clickItem3.count);
        PlayerPrefs.SetInt(clickPower3, clickItem3.clickPower);
        PlayerPrefs.SetFloat(cost3, clickItem3.cost);
        // Item 4 //
        PlayerPrefs.SetInt(count4, clickItem4.count);
        PlayerPrefs.SetInt(clickPower4, clickItem4.clickPower);
        PlayerPrefs.SetFloat(cost4, clickItem4.cost);
        // Item 5 //
        PlayerPrefs.SetInt(count5, clickItem5.count);
        PlayerPrefs.SetInt(clickPower5, clickItem5.clickPower);
        PlayerPrefs.SetFloat(cost5, clickItem5.cost);
    }

    // Loads all variables
    public void LoadGame()
    {
        // Load Variables to UpgradeManager Script
        // Item 1 //
        clickItem1.clickPower = PlayerPrefs.GetInt(clickPower1);
        clickItem1.count = PlayerPrefs.GetInt(count1);
        clickItem1.cost = PlayerPrefs.GetFloat(cost1);
        // Item 2 //
        clickItem2.clickPower = PlayerPrefs.GetInt(clickPower2);
        clickItem2.count = PlayerPrefs.GetInt(count2);
        clickItem2.cost = PlayerPrefs.GetFloat(cost2);
        // Item 3 //
        clickItem3.clickPower = PlayerPrefs.GetInt(clickPower3);
        clickItem3.count = PlayerPrefs.GetInt(count3);
        clickItem3.cost = PlayerPrefs.GetFloat(cost3);
        // Item 4 //
        clickItem4.clickPower = PlayerPrefs.GetInt(clickPower4);
        clickItem4.count = PlayerPrefs.GetInt(count4);
        clickItem4.cost = PlayerPrefs.GetFloat(cost4);
        // Item 5 //
        clickItem5.clickPower = PlayerPrefs.GetInt(clickPower5);
        clickItem5.count = PlayerPrefs.GetInt(count5);
        clickItem5.cost = PlayerPrefs.GetFloat(cost5);
    }
}