How To Update Text In Unity
- Spaces
- Default
- Help Room
- META
- Moderators
-
- Topics
- Questions
- Users
- Badges
- Domicile /
0
Modify text value
I am just starting to write C# and i would use Javascript if i could because my knowledge almost C# specifically is "pocket-sized".
So, i have a UI Text, and i am trying to make information technology add together 1 to the score each time the player clicks a button. For example, the score is 0. The role player clicks the push button, and it is changed to one. If he clicks again, information technology changes to two and it continues. I have tried A LOT OF THINGS simply i always got myself in the same two problems: i tin't reference something or i tin can't understand how information technology works and so i can make it. I think i am in the right path, but i couldn't really find annihilation related to how to reference texts and how to make a variable for it, and i am as well struggling in making a trigger for it.
Hither is my code for now:
using UnityEngine; using Organisation.Collections; public class ObjClick : MonoBehaviour { bool click = false; public GameObject fall; public GameObject txt; //I wanted to use this variable to hold the text, just every bit i said i couldn't reference it. There isn't exactly something like "public Text txt". public Animator anim; public int score; void Start() { fall.SetActive(simulated); } void Update() { if (Input.GetButtonDown("Fire1")) { anim.SetTrigger("Activity"); autumn.SetActive(true); //I plan to make something like, on left mouse click, add one to the score. } } void UpdateScore() { txt.text = score; // <- This is my chief problem. I don't know how to reference the text. I e'er get the fault "Blazon 'UnityEngine.GameObject' does not incorporate a definition for text" etc... } }
Could someone aid me to finish this? Information technology's very important. Any help would be appreciated. Btw this is my last question.
3 Replies
· Add together your reply
- Sort:
12
Reply past Prezzo · Nov 18, 2016 at 02:38 PM
To start, Include UnityEngine.UI then write using UnityEngine.UI
on elevation of your script.
Adjacent, alter public GameObject txt;
by public Text txt;
In the editor, drag your text object into the text slot of your script.
Access your text of your Txt object by typing txt.text = 'blah blah text'
. For displaying numbers, yous need to convert them to a cord first, so write
txt.text = yourNumberVariable.ToString()
1
Reply by ElijahShadbolt · Nov 18, 2016 at 06:20 AM
txt.GetComponent<UnityEngine.UI.Text>().text = score.ToString();
0
Respond past giantkilleroverunity3d · Jan 18, 2019 at 06:43 PM
@Zitoox The text box is in the inspector panel of Unity. Thought I would state this as the OP comment posted that
In that location isn't exactly something like "public Text txt".
Your answer
Welcome to Unity Answers
The all-time place to enquire and answer questions near development with Unity.
To help users navigate the site we have posted a site navigation guide.
If you are a new user to Unity Answers, cheque out our FAQ for more information.
Make sure to bank check out our Noesis Base of operations for usually asked Unity questions.
If yous are a moderator, see our Moderator Guidelines page.
We are making improvements to UA, see the list of changes.
Follow this Question
Related Questions
Source: https://answers.unity.com/questions/1273054/change-text-value.html
Posted by: keyyouts1951.blogspot.com
Which do you mean by 'UI Text'? Is this as part of an nGUI canvas, a legacy GUI Text component, or a custom script? Each is accessed differently. Side note; text isn't a fellow member of the GameObject class, yous would admission it similar near components in Unity 5, i.due east.
txt.GetComponent();
You would also want to add ".ToString()" after the 'score' assignment, otherwise you'll get an mistake there.
It's a text, and it is a child of canvas. Normal text.