Search Unity

after i bought an store in my game the money goes on an number and not on dollar??

Discussion in 'Scripting' started by nielsmeijer12345, Jan 23, 2020.

  1. nielsmeijer12345

    nielsmeijer12345

    Joined:
    Nov 15, 2019
    Posts:
    2
    in my game i have stores but when i buy 1 store the money goes from $2.5 to 0 so there is not an $ at the beginning of the number does anyone know why and how??

    this is my script



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

    public class script : MonoBehaviour
    {
    float BaseStoreCost;
    float CurrentBalance;
    float BaseStoreProfit;


    int StoreCount;
    public Text StoreCountText;
    public Text CurrentBalancetext;

    float StoreTimer = 1f;
    float currentTimer = 0;
    bool StartTimer;

    // Start is called before the first frame update
    void Start()
    {
    StoreCount = 0;
    CurrentBalance = 2.50f;
    BaseStoreCost = 2.50f;
    BaseStoreProfit = 0.50f;
    CurrentBalancetext.text = CurrentBalance.ToString("C2");
    StartTimer = false;
    }

    // Update is called once per frame
    void Update()
    { if (StartTimer)
    {
    currentTimer += Time.deltaTime;
    if (currentTimer > StoreTimer)

    {



    {
    StartTimer = false;
    currentTimer = 0f;
    CurrentBalance += BaseStoreProfit * StoreCount;
    CurrentBalancetext.text = CurrentBalance.ToString("C2");
    }

    }
    }
    }

    public void BuyStoreOnClick()
    {
    if (BaseStoreCost > CurrentBalance)
    return;
    StoreCount = StoreCount + 1;
    Debug.Log(StoreCount);
    StoreCountText.text = StoreCount.ToString();
    CurrentBalance = CurrentBalance - BaseStoreCost;
    Debug.Log(CurrentBalance);
    CurrentBalancetext.text = CurrentBalance.ToString();
    }


    public void StoreOnClick()
    {

    Debug.Log("clicked the store");
    if (!StartTimer)
    StartTimer = true;


    }


    }
     
  2. ColinDarkwind

    ColinDarkwind

    Joined:
    Nov 26, 2013
    Posts:
    8
    In your Start function, you set the value of
    CurrentBalancetext.text
    to
    CurrentBalance.ToString("C2")
    but later in your code (in the BuyStoreOnClick function) you are setting the text to
    CurrentBalance.ToString();


    The "C2" passed to the ToString function in the Start specifies how it should be formatted. In your case, "C2" makes it a Currency format with two decimal digits (C for currency, 2 for precision)

    So, in BuyStoreOnClick, just change
    CurrentBalancetext.text = CurrentBalance.ToString();
    to
    CurrentBalancetext.text = CurrentBalance.ToString("C2");


    For more information on formats: Standard Numeric Format Strings
     
    Joe-Censored likes this.
  3. nielsmeijer12345

    nielsmeijer12345

    Joined:
    Nov 15, 2019
    Posts:
    2
    omg i dindt see that thank you <3