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?
You need a reference to the Text object, then you can change the text through script.
using UnityEngine.UI; // Don't forget this line
public class Script : MonoBehaviour
{
public Text counterText;
public float counter;
public void Update()
{
counterText.text = counter.ToString();
}
}
Thank you so much,worked fine!!!
I was making an ammo counter for an fps game and it worked great thank you
This helped me a bunch! thank you
Helped me too! Very simple! Thanks!!!
May I ask I am really really really new to unity and I am using textmesh pro how do I connect the variable to the text
Please don’t necro-post. If you have a new question, make a new post. It’s FREE!!
Start with a tutorial, perhaps one from Unity’s “Learn” section at the top of this page.
If a tutorial isn’t sufficient, this tiny text box won’t help you either.
Two steps to tutorials and / or example code:
- do them perfectly, to the letter (zero typos, including punctuation and capitalization)
- stop and understand each step to understand what is going on.
If you go past anything that you don’t understand, then you’re just mimicking what you saw without actually learning, essentially wasting your own time. It’s only two steps. Don’t skip either step.
If you absolutely get stumped, try framing your question like this:
How to report your problem productively in the Unity3D forums:
This is the bare minimum of information to report:
- what you want
- what you tried
- what you expected to happen
- what actually happened, log output, variable values, and especially any errors you see
- links to actual Unity3D documentation you used to cross-check your work (CRITICAL!!!)
The purpose of YOU providing links is to make our job easier, while simultaneously showing us that you actually put effort into the process. If you haven’t put effort into finding the documentation, why should we bother putting effort into replying?
If you post code, only post the relevant code and always use the format button above. Do not post photographs of code.