- Home /
Which type of Variable do I need to use to change the Value of a text UI object ?
I am trying to display the Score of my player. I set up the reference to the Playermovement Script where the Scorecounting takes place and I converted the Score to a string type. I thought it would work like this but im getting the following error : Assets/ScoreDisplay.cs(20,17): error CS0029: Cannot implicitly convert type string' to
UnityEngine.UI.Text' So I am assuming that string is not the right type. could someone explain what i did wrong and give a Solution ? Thanks heres my Script :
using UnityEngine; using System.Collections; using UnityEngine.UI;
public class ScoreDisplay : MonoBehaviour {
Text text;
private string score;
Playermovement playermovement;
void Start () {
text = GetComponent<Text> ();
playermovement = GetComponent<Playermovement> ();
}
void Update () {
score = playermovement.playerscore.ToString();
text = "Score:" + score;
}
}
(I am german. feel free to correct my english)
Answer by NoseKills · Jun 28, 2015 at 02:21 PM
In your code you have this
Text text;
...
text = "Score:" + score;
You are attempting to store a variable of type string
into a variable of type Text
(unity UI text field) which is like trying to do int number = "Hello"
What you actually want to do is let the Text
component know that you want it to display your string. To do that you would write
Text text;
...
text.text = "Score:" + score;
I recommend you rename you Text text
variable to something like Text textLabel
to avoid confusion. It is much more self explanatory to write textLabel.text = "Hello";
Don't forget to validate his answer if it did answered your question.
Your answer
Follow this Question
Related Questions
How to change UI button of a particular gameobject to another UI button 0 Answers
Adjusting UI to cellphone screen size 0 Answers
How do i make a BattleLog? 0 Answers
Glowing text effect 0 Answers
Line break UI Text field 1 Answer