- Home /
How to edit UI Text from script
Hi! I've got a problem with editing new UI Text from script. I attached this (part of) script to my UI Text gameObject:
Text score;
void Start()
{
score = GetComponent<Text>();
score.text += objects.Player_Score;
}
and I received this compilation fail:
Assets/Scripts/Menu/gameOverScoreDisplay.cs(6,9): error CS0246: The type or namespace name `Text' could not be found. Are you missing a using directive or an assembly reference?
What should I do to use 'Text' type in my scripts? I saw the same part of script in the official Unity video (LINK)... Yes, I've got the newest Unity update (4.6).
Answer by Deadcow_ · Dec 06, 2014 at 04:36 PM
You need to put reference to namespace, where new UI classes contains:
using UnityEngine.UI;
Or you also can refer to this type as
UnityEngine.UI.Text score;
and
score = GetComponent<UnityEngine.UI.Text>();
can you please help me with this in JS ? am new and not enough tutorials about this.
Answer by dikabeast · Dec 06, 2014 at 07:08 PM
Maybe you forget adding using UI
using UnityEngine.UI;
About editing text in UI from script you can see here : http://answers.unity3d.com/questions/795069/46-ui-accessing-text-in-image-inside-canvas-via-c.html
Answer by yoppuyoppu · Aug 08, 2017 at 08:44 PM
If you're having trouble using JS, try the following:
// at the top of your code where you declare variables
var score: UnityEngine.UI.Text;
// no need to do anything inside Start() unless you would like to set an initial value