Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

How to display variable in text?

Discussion in 'Scripting' started by BL4CK0UT19, Mar 13, 2020.

  1. BL4CK0UT19

    BL4CK0UT19

    Joined:
    Aug 21, 2019
    Posts:
    75
    Yes,my question is very simple,but i just don't know how to do it.
    I have a textUI in my scene.
    In another script,i have a variable that counts the number of bullets the player has.
    I simply want to display that variable in my textUI.
    How can i do that?
     
  2. WallaceT_MFM

    WallaceT_MFM

    Joined:
    Sep 25, 2017
    Posts:
    394
    You need a reference to the Text object, then you can change the text through script.
    Code (csharp):
    1. using UnityEngine.UI; // Don't forget this line
    2. public class Script : MonoBehaviour
    3. {
    4.    public Text counterText;
    5.    public float counter;
    6.  
    7.    public void Update()
    8.    {
    9.       counterText.text = counter.ToString();
    10.    }
    11. }
     
  3. BL4CK0UT19

    BL4CK0UT19

    Joined:
    Aug 21, 2019
    Posts:
    75
    Thank you so much,worked fine!!!!!!!!
     
  4. Cortlandia

    Cortlandia

    Joined:
    May 31, 2021
    Posts:
    1
    I was making an ammo counter for an fps game and it worked great thank you
     
  5. Eatnine

    Eatnine

    Joined:
    Feb 14, 2022
    Posts:
    2
    This helped me a bunch! thank you
     
  6. unity_A95C530FB63D3FFE79B5

    unity_A95C530FB63D3FFE79B5

    Joined:
    Jun 8, 2022
    Posts:
    1
    Helped me too! Very simple! Thanks!!!