- Home /
The question is answered, right answer was accepted
[4.6 - UI] Changing the text component via script?
Hi all, I hope someone in the community can help me on this.
I've been using 4.6 for awhile now, mainly testing out the new UI system. However, I can't seem to find any good documentation on how to change any of the components via script.
Does somebody know how I can change the Text component's text? I've placed it inside the canvas and added a script that has a gameobject variable. Set that in the editor as the Text gameobject but can't seem to see any relative information in VS2013's intellisense.
Many thanks.
Since this has made it back to the front page I'll put a link to the documentation here.
http://docs.unity3d.com/460/Documentation/$$anonymous$$anual/
I just tried all your solutions too.... I am using Unity Script. "The name 'Text' does not denote a valid type ('not found'). Did you mean 'UnityEngine.UI.Text'?
I had to do declare it as UI.Text to make it work >>
var buttonText: UI.Text;
// ==================//
then
buttonText.text="46";
add "using UnityEngine.UI;" in your header and everything should be accessible, thanks for your answer, otherwise I still would be searching for the solution! :)
Why are you not in first position ! ?
Thx for this answer ^^
Answer by mlabarca · Aug 24, 2014 at 02:14 PM
Hi, First you would need to add this import: using UnityEngine.UI; Then, you can declare a Text field and later use getComponent to initialize it;
Text instruction;
void Start () {
instruction = GetComponent<Text>();
}
You would then be able to reference the instruction.text property and change it's string to whatever you like, or any other properties. Of course, this would be a script attached to a gameobject with the text field.
I found this while taking a look at ShowSliderValue.cs which is included with the UI example project provided by Unity this week.
Thanks, I know how to access components. Hopefully this question will help others. :)
Thank you. Currently the manual has no links to switch from Text's manual to Scripting, and neither is the other way around. Probably a documentation bug/oversight on Unity's part.
@mlabarca I have 2 questions regarding your awesome answer:
What if you have more than one line of text?, how do you edit one especific line of text?, I.E: $$anonymous$$ENU
CONTINUE NEW OPTIONS QUIT
How do you edit the color of the text?
Answer by Nick4 · Aug 24, 2014 at 10:02 AM
You'll need to use UnityEngine.UI namespace, so add this on top of your script :
using UnityEngine.UI
This is exactly what I was looking for half an hour! There are a lot of tutorials where GUIText is still used. $$anonymous$$ost of the time the snippets you can find for ui elements are also using GUIText. Im wondering, the UI
package is not imported automatically if using UnityEngine
is already included in the header.
Answer by HVgame · Aug 24, 2014 at 02:40 PM
first
using UnityEngine.UI;
and
public Text Bro;
void Update () { Bro.text = "Get Back!"; }
Answer by 0mr_ashutosh0 · Nov 14, 2014 at 12:33 PM
IMPORTANT: This is attached to the text component of my button in UI canvas
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class DisplayScore : MonoBehaviour {
Text txt;
private int currentscore=0;
// Use this for initialization
void Start () {
txt = gameObject.GetComponent<Text>();
txt.text="Score : " + currentscore;
}
// Update is called once per frame
void Update () {
txt.text="Score : " + currentscore;
currentscore = PlayerPrefs.GetInt("TOTALSCORE");
PlayerPrefs.SetInt("SHOWSTARTSCORE",currentscore);
}
}
Answer by tvranjith · Mar 13, 2015 at 06:23 PM
How unity implements the UI is just like any other unity components. You add GameObject and attach UI components to it. Once you have the game object you can get the UI components from it and change the properties.
API reference for UI can be found under UnityEngine.UI namespace, API reference for BUtton is http://docs.unity3d.com/ScriptReference/UI.Button.html
See the post http://chikkooos.blogspot.jp/2015/03/new-ui-implementation-using-c-scripts.html for more information on accessing UI from C# scripts
Correct. But even you are answering the question, the problem is, the Text
object could not be accessed as long as using UnityEngine.UI;
was not included in the header. Think most of the problem is solved by including the UI package.
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Solutions for dynamic interface 0 Answers