0

I am having trouble to update my field/variable properly. Simply my problem is: when I call AddCoins(); method, I can see on Unity that coins are increasing on the scene. However for some reason CheckPurchaseable(); can not detect the updated value of coins but works on the starting value of coins therefore "myPurchaseButtons[i].interactable = true;" never become true.

In order to manipulate any confusion in the code I wanted to use "this.coin" I am getting error of "CS0176 Static member 'member' cannot be accessed with an instance reference; qualify it with a type name instead"

why do you think , when AddCoins(); can reach the and update the values of coins where CheckPurchaseable(); cannot read the updated value of coins?

thank you

using TMPro;
using UnityEngine;
using UnityEngine.UI;

public class ShopManager : MonoBehaviour
{
    public static double coins { get; set; } = 0;
    public TextMeshProUGUI coinsUI;
    public ShopItemScriptableObject[] shopItemSO;
    public GameObject[] shopPanelsGO;
    public ShopTemplate[] shopPanels;
    public Button[] myPurchaseButtons;

    void Start()
    {
        for (int i = 0; i < shopItemSO.Length; i++)
        {
            shopPanelsGO[i].SetActive(true);
        }
        LoadPanels();
        CheckPurchaseable();
    }

    public void Update()
    {
        UITexts();
    }

    public void AddCoins()
    {
        coins += 20;
        CheckPurchaseable();
    }

    public void CheckPurchaseable()
    {
        for (int i = 0; i < shopItemSO.Length; i++)
        {
            //Debug.Log("cost: " + shopItemSO[i].name + shopItemSO[i].baseCost);

            if (coins >= shopItemSO[i].baseCost) // if i have enough money
            {
                myPurchaseButtons[i].interactable = true;
            }
            else
            {
                myPurchaseButtons[i].interactable = false;
            }
        }
    }

    public void PurchaseableItem(int buttonNo)
    {
        if (coins >= shopItemSO[buttonNo].baseCost)
        {
            coins -= shopItemSO[buttonNo].baseCost;
            CheckPurchaseable();
        }
    }

    public void LoadPanels()
    {
        for (int i = 0; i < shopItemSO.Length; i++)
        {
            shopPanels[i].titleText.text = shopItemSO[i].title;
            shopPanels[i].descriptionText.text= shopItemSO[i].description;
            shopPanels[i].costText.text = "Coins: " + shopItemSO[i].baseCost.ToString();
        }
    }

    public void UITexts()
    {
        coinsUI.text = "Coins:  " + coins.ToString();
    }

}
  • 1
    in general ... does it need to be a `double`? Sounds like `coins` should be an `int` .. never have heard of half coins ;) And well `coins` is `static` so you can not access it via an instance (which is what the error is telling you) .. why is it `static` at all? ... Have you tried [debugging your code](https://docs.unity3d.com/Manual/ManagedCodeDebugging.html) ? – derHugo Sep 26 '22 at 07:10
  • really should you focus on the type? I am working on it still, since it is the market system coins represents the money there. so an item can cost $3.9879078. But for now I am testing so I don't need to use floating numbers. good luck criticizing without thinking next step. –  Sep 26 '22 at 10:35
  • Ok .. there is no need to get pissed here ... What I did was no criticizing .. if you look at my comment it is a question. If you deal with such numbers you would probably use rather a `decimal`, though ^^ Types aside .. there is no way that `coins` is `0` after you do `+= 20` .. except it was `-20` before for which I see no reason ... – derHugo Sep 26 '22 at 10:43
  • ok, instead lets say I use "public double coins = 600;" it is still working same. if you have any solution for "public double coins = 600;" that will be great. why I use static there because I have persistence manager (save/load system) where I will save and load this coins ( value of money) using something like below: ``` public void LoadData(GameData data) { coins = data.coins; } public void SaveData(GameData data) { data.coins = coins; } ``` –  Sep 26 '22 at 11:34
  • as said you will need to debug this .. from my point of view here there is nothing obvious in your code that would cause the behavior you are describing .. there would if it would not be `static` then I would claim something is overwriting the UI display where the value wasn't set ... If you want to be sure what exactly happens I would reccomand you do `private static double _coins; public static double coins { get { return _coins; } set { _coins = value; } }` and set a breakpoint into the setter and getter – derHugo Sep 26 '22 at 11:44
  • (1) I ll do that now. I want to give some more details: I am working with scripable objects that every of them has base price (suppose 100 coins(dollar)) and have purchase button which calls " public void PurchaseableItem(int buttonNo)". suppose we call the first scriptable object's purchase button so it assign index number 0. then "public void PurchaseableItem(0) will bring value of 100 and check if I have enough money to buy 100 coins valued item. if yes its is going to change the value of coins 100 down. –  Sep 26 '22 at 12:00
  • (2)Here is the problem comes: if I start with lets say 300 coins on the code it works but when I call AddCoins(); to have some more coins I can see it is increasing the coins on the editor but my purchase buttons are not get activated back. means: coins value under "public void CheckPurchaseable()" method working differently then coins value under the AddCoins(); method –  Sep 26 '22 at 12:00
  • (3) I have AddCoins(); here just to test purpose. Later I will change it to real money that player can buy through AppStore/Google store. –  Sep 26 '22 at 12:03
  • Are you sure that your buttons are all connected to the correct scriptable object? It looks quite risky to simply assume that both lists/arrays are in the correct order etc ... – derHugo Sep 26 '22 at 15:16
  • I am done now, thanks. actually I could not handle my problem inside the code but taking the values out as json and loading back from text file helped me to solve the issue –  Sep 27 '22 at 12:17

2 Answers2

0

because of this: public static double coins { get; set; } = 0;

your coins are 0 when you start the game. try saving the coins value in a separate json/prefs and load its value in the Start function. the code seems to be doing exactly how it is supposed to be.

0

I solved the confusion by saving all coin data to a text file with already in use data persistence system. Thus data is not getting into a conflict and saved and loaded from text file.

Anyone who may have similar problem, may comment below I will be happy to help.

    public void LoadData(GameData data)
    {
        coins = data.coins;

    }

    public void SaveData(GameData data)
    {
        data.coins = coins;


    }