I am having a problem with my money updates when substraction them.

public class UIManager_2 : MonoBehaviour
{
    public static UIManager_2 instance;
    public GameObject MapSelectionPanel;
    [Header("Our STAR UI")]
    public int Stars;

    public Text starText;

    public int Moneys2;
    public int Moneys3;
    public Text moneyText2;

    public int Item1;
    public GameObject shopPanel;
    public Text Priceitem;
    public Text myMoney;
  


    public void Awake()
    {
        instance = this;

    }

    public void Update()
    {
        UpdateStarUI();
        UpdateNewMoney();
        moneyText2.text = Moneys2.ToString() + (" B");
        myMoney.text = Moneys2.ToString() + (" B");
        Priceitem.text = "Price :"+Item1.ToString() ;
        Updatemoney3();
    }
    public void UpdateStarUI()
    {
        Stars = PlayerPrefs.GetInt("Star" + 1) + PlayerPrefs.GetInt("Star" + 2) + PlayerPrefs.GetInt("Star" + 3) + PlayerPrefs.GetInt("Star" + 4)
            + PlayerPrefs.GetInt("Star" + 5) + PlayerPrefs.GetInt("Star" + 6) + PlayerPrefs.GetInt("Star" + 7) + PlayerPrefs.GetInt("Star" + 8);
        starText.text = Stars.ToString() + ("/24");
    }
    public void UpdateNewMoney()
    {     
        Moneys2 = PlayerPrefs.GetInt("Money" + 1) + PlayerPrefs.GetInt("Money" + 2) + PlayerPrefs.GetInt("Money" + 3) + PlayerPrefs.GetInt("Money" + 4)
          + PlayerPrefs.GetInt("Money" + 5) + PlayerPrefs.GetInt("Money" + 6) + PlayerPrefs.GetInt("Money" + 7) + PlayerPrefs.GetInt("Money" + 8);
        Debug.Log("Money" + Moneys2);
    }


    public void BuyItem()
    {

        if (Moneys2 >= Item1)
        {
            Moneys3 = Moneys2 - Item1;
            //PlayerPrefs.SetInt("Money",Moneys2);
            Debug.Log("MoneySubStrac :" + Moneys3);
        }
        else
        {
            Debug.Log("NoMoney");
        }
      
    }
    public void Updatemoney3()
    {
        Moneys3 += Moneys2;
        Debug.Log("newMoney :" + Moneys3);
    }

I want to earn money from external classes using PlayerPrefs and subtract the value of the item. I have been chasing for several days and don’t understand.

To help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

Doing this should help you answer these types of questions:

  • is this code even running? which parts are running? how often does it run?
  • what are the values of the variables involved? Are they initialized?

Knowing this information will help you reason about the behavior you are seeing.

Also, what are you doing in line 44? Do you really want lots of PlayerPrefs integers called “Money1” , “Money2” etc.??

You might want to use an array and get it all working WITHOUT player prefs, then when it works put the array into another class, serialize it to a string with JSON, and write that to a single PlayerPrefs string.

appropriatedistinctdegu

/// main class ///////
private void OnTriggerEnter(Collider hit)
    {
        if (hit.CompareTag ("Coin"))
        {
            collidedCoinValue = hit.gameObject.GetComponent<CoinValue>().coinValue1;
            MoneyAmount += collidedCoinValue;
            collidedCoinValue2 = hit.gameObject.GetComponent<CoinValue>().coinValue2;
            CoinAmount += collidedCoinValue2;
            Destroy(hit.gameObject);
        }
    }
PlayerPrefs.SetInt("Money" + mapIndex, MoneyAmount + PlayerPrefs.GetInt("Money" + mapIndex));

/////recive class///
public class UIManager_2 : MonoBehaviour
{
    public static UIManager_2 instance;
    public GameObject MapSelectionPanel;
    [Header("Our STAR UI")]
    public int Stars;

    public Text starText;

    public int Moneys2;
    public Text moneyText2;
    public GameObject shopPanel;
    public Text Priceitem;
    public int Item1;
    public Text myMoney;
 


    public void Awake()
    {
        instance = this;
    }
    private void Start()
    {
        Moneys2 = PlayerPrefs.GetInt("Money" + 1) + PlayerPrefs.GetInt("Money" + 2) + PlayerPrefs.GetInt("Money" + 3) + PlayerPrefs.GetInt("Money" + 4)
    + PlayerPrefs.GetInt("Money" + 5) + PlayerPrefs.GetInt("Money" + 6) + PlayerPrefs.GetInt("Money" + 7) + PlayerPrefs.GetInt("Money" + 8);
        PlayerPrefs.SetInt("Money", Moneys2);
        Debug.Log("Money :" + PlayerPrefs.GetInt("Money"));
    }
    public void Update()
    {
        UpdateStarUI();
         moneyText2.text = PlayerPrefs.GetInt("Money").ToString() + (" B");
        myMoney.text = PlayerPrefs.GetInt("Money").ToString() + (" B");
        Priceitem.text = "Price :"+Item1.ToString() ;
    }
    public void UpdateStarUI()
    {
        Stars = PlayerPrefs.GetInt("Star" + 1) + PlayerPrefs.GetInt("Star" + 2) + PlayerPrefs.GetInt("Star" + 3) + PlayerPrefs.GetInt("Star" + 4)
            + PlayerPrefs.GetInt("Star" + 5) + PlayerPrefs.GetInt("Star" + 6) + PlayerPrefs.GetInt("Star" + 7) + PlayerPrefs.GetInt("Star" + 8);
        starText.text = Stars.ToString() + ("/24");
    }
 
    public void BuyItem()
    {
      
        if (PlayerPrefs.GetInt("Money") >= Item1)
        {
            PlayerPrefs.SetInt("Money", PlayerPrefs.GetInt("Money")-Item1);

              Debug.Log("MoneySub :" + PlayerPrefs.GetInt("Money"));
         
        }
        else
        {
            Debug.Log("NoMoney");
        }
     
    }

Should I use PlayerPrefs? Or use other than this What should I use to collect my money?