- Home /
Change text value
I am just starting to write C# and i would use Javascript if i could because my knowledge about C# specifically is "small".
So, i have a UI Text, and i am trying to make it add 1 to the score each time the player clicks a button. For example, the score is 0. The player clicks the button, and it is changed to one. If he clicks again, it changes to two and it continues. I have tried A LOT OF THINGS but i always got myself in the same two problems: i can't reference something or i can't understand how it works so i can make it. I think i am in the right path, but i couldn't really find anything related to how to reference texts and how to make a variable for it, and i am also struggling in making a trigger for it.
Here is my code for now:
using UnityEngine;
using System.Collections;
public class ObjClick : MonoBehaviour
{
bool click = false;
public GameObject fall;
public GameObject txt; //I wanted to use this variable to hold the text, but as 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(false);
}
void Update()
{
if (Input.GetButtonDown("Fire1"))
{
anim.SetTrigger("Action");
fall.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 main problem. I don't know how to reference the text. I always get the error "Type 'UnityEngine.GameObject' does not contain a definition for text" etc...
}
}
Could someone help me to finish this? It's very important. Any help would be appreciated. Btw this is my last question.
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 member of the GameObject class, you would access it like most components in Unity 5, i.e.
txt.GetComponent();
You would also want to add ".ToString()" after the 'score' assignment, otherwise you'll get an error there.
It's a text, and it is a child of canvas. Normal text.
Answer by Prezzo · Nov 18, 2016 at 02:38 PM
To start, Include UnityEngine.UI so write using UnityEngine.UI
on top of your script.
Next, change 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, you need to convert them to a string first, so write
txt.text = yourNumberVariable.ToString()
I got stuck at the "In the editor, drag your text object into the text slot of your script." because Unity says I am unable to drag it. Please help :'(
Answer by ElijahShadbolt · Nov 18, 2016 at 06:20 AM
txt.GetComponent<UnityEngine.UI.Text>().text = score.ToString();
Answer by 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
There isn't exactly something like "public Text txt".
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
I need help with a Text Location script (Ho bisogno di aiuto con uno script di posizione del testo) 0 Answers
Making texts appear/disappear after having completed a task 0 Answers
Unity 2D - Text UI and Textbox scripting issue - HELP ASAP! 2 Answers