Home > Software design >  Updating Field/ variable acting differently in the code and UNITY
Updating Field/ variable acting differently in the code and UNITY

Time:09-28

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

}

CodePudding user response:

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.

CodePudding user response:

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;


    }
  • Related