How to make a stockpile script?

I made a script where you collect coins, the issue is that the coin variable will always be set back to 0 for gameplay purposes so I created another variable for stockpiling the coins the player collected. I don’t understand why when I collect 12 coins, the actual amount in the shop is above 1,000. I need it to stockpile accurately, just basic addition.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class NewMechanics : MonoBehaviour
{
public Text coinText;
private float coins;
public Text stockText;
private float stockValue = 0f;

void Start()
{
    coins = 0f;
}

// Update is called once per frame
void FixedUpdate()
{
    stockText.text = stockValue.ToString("0,000");

    stockValue += coins;

    coinText.text = coins.ToString("0");
}

 void OnTriggerEnter (Collider col)

{
if (col.gameObject.tag == “coin”)
{
coins += 1f;
}
}

If stockValue is your variable for stockpiling the coins, you are adding to it every fixedupdate, so if coins was 12, you would be adding 12 to it pretty often. Say stockValue was 100, coins is 12, you got to fixedupdate now its 112, next fixed update its 124, and that pattern continues. Add 1 to your stockValue where you increase your coins, and remove the increment in fixedupdate. It should function like coins but without the re-setting, so whenever you collect a coin it goes up by that amount but never goes to zero.

If you want to have commas in the thousands place, don’t do this stockValue.ToString(“0,000”);


Do this instead stockValue.ToString(“N0”);

Maybe that is your issue. Hope it helps.