- Home /
Inputting a variable into UI Text
Is there anyway to take my score variable and display on a UI Text created in the Hierarchy? Thanks!
Your UI Text object should have something like obj.Text to set string values. You need to explain a bit more.
Answer by Srki94 · Dec 24, 2014 at 02:25 AM
Yeah there is, you can access any component from any gameObject. Since you didn't tell us are you using old system or new one, I wrote this code for new one :
GameObject myTextgameObject; // gameObject in Hierarchy
Text ourComponent; // Our refference to text component
void Start () {
// Find gameObject with name "MyText"
myTextgameObject = GameObject.Find("MyText");
// Get component Text from that gameObject
ourComponent = myTextgameObject.GetComponent<Text>();
// Assign new string to "Text" field in that component
ourComponent.text = "Hey, I'm some randoms score !!";
}
If you set myTextgameObject as public, you can drag & drop it in inspector. This code assumes that you don't know where is that object so we find it through code.
This is C#, you also didn't state which language are you using but it should be easy from here with documentation.
P.S. You will need using UnityEngine.UI; before namespace.
Sorry about not stating those things but thats exactly what i needed thanks so much!
Answer by shriya · Dec 24, 2014 at 04:53 AM
HI,If you are working with new UI then in scripting you need to do following
using UnityEngine.UI; //its a must to access new UI in script
public class YourClass : MonoBehaviour
{
public Text Score_UIText; // assign it from inspector
void Start()
{
Score_UIText.text = yourscore_variable;
}
}
Your answer
Follow this Question
Related Questions
¿Score system with the new UI? 0 Answers
How to make an online scoreboard -1 Answers
Adding coin score to multiplied value 2 Answers
Show score on gameover screen? 1 Answer
Count scripting problem 1 Answer