Skip to content
TheChatArchiveThe Chat Archive
gpt-4-0314· openaiHugging Face Suzhen/CodeChat

From the current code snippet string currencyID = "FOX_COIN"; CurrencyDefinition…

  1. User

    From the current code snippet string currencyID = "FOX_COIN"; CurrencyDefinition goldCurrencyDefinition = await EconomyService.Instance.Configuration.GetCurrencyAsync(currencyID); PlayerBalance playersGoldBarBalance = await goldCurrencyDefinition.GetPlayerBalanceAsync(); how do i make it display the players balance on a text gameobject

  2. Assistant

    First, you need a reference to the Text component from the UnityEngine.UI namespace in your script. You can do this by creating a public variable so that you can assign the Text component from the Unity editor. Then, you can simply set the Text property of the component with the player's balance value.

    Here's an example script:

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.UI;
    using Economy;
    
    public class DisplayPlayerBalance : MonoBehaviour
    {
        public Text playerBalanceText; // Reference to the Text component
    
        private async void Start()
        {
            string currencyID = "FOX_COIN";
            CurrencyDefinition goldCurrencyDefinition = await EconomyService.Instance.Configuration.GetCurrencyAsync(currencyID);
            PlayerBalance playersGoldBarBalance = await goldCurrencyDefinition.GetPlayerBalanceAsync();
            
            // Set the text component to display the player's balance
            playerBalanceText.text = "Player's Balance: " + playersGoldBarBalance.Balance.ToString();
        }
    }
    
    1. Add this script to the desired game object in the Unity editor.
    2. Make sure you have a Text component created in the scene (you can create it using Create > UI > Text) and set its RectTransform to your desired position and size.
    3. Assign the Text component to the "playerBalanceText" variable in the inspector for DisplayPlayerBalance script on your game object.
  3. User

    how do I attach my soft and premium currencies to text game objects using this snippet List definitions = EconomyService.Instance.Configuration.GetCurrencies();

  4. Assistant

    You can loop through the definitions list to find the Soft and Premium CurrencyDefinition, and then use their balances to set the text for different Text components.

    Here's an example script:

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.UI;
    using Economy;
    
    public class DisplayCurrencies : MonoBehaviour
    {
        public Text softCurrencyText; // Reference to Text component for Soft Currency
        public Text premiumCurrencyText; // Reference to Text component for Premium Currency
    
        private async void Start()
        {
            List<CurrencyDefinition> definitions = EconomyService.Instance.Configuration.GetCurrencies();
    
            foreach (CurrencyDefinition currency in definitions)
            {
                // Replace "SOFT_CURRENCY_ID" and "PREMIUM_CURRENCY_ID" with the actual currency IDs you are using.
                if (currency.CurrencyID == "SOFT_CURRENCY_ID")
                {
                    PlayerBalance softCurrencyBalance = await currency.GetPlayerBalanceAsync();
                    softCurrencyText.text = "Soft Currency: " + softCurrencyBalance.Balance.ToString();
                }
                else if (currency.CurrencyID == "PREMIUM_CURRENCY_ID")
                {
                    PlayerBalance premiumCurrencyBalance = await currency.GetPlayerBalanceAsync();
                    premiumCurrencyText.text = "Premium Currency: " + premiumCurrencyBalance.Balance.ToString();
                }
            }
        }
    }
    
    1. Add this script to the desired game object in the Unity editor.
    2. Make sure you have two Text components created in the scene for Soft and Premium Currencies (you can create them using Create > UI > Text) and set their RectTransform to your desired positions and sizes.
    3. Assign the Soft Currency Text component to the "softCurrencyText" variable and the Premium Currency Text component to the "premiumCurrencyText" variable in the inspector for DisplayCurrencies script on your game object.