- Home /
Script to change GUI text's displayed text value?
Hi. I want to use GUI Text to display a score, but it needs to change. Can I please have a basic code snippet to change the displayed text from a variable?
Thanks!
Answer by e-bonneville · Apr 06, 2011 at 01:02 AM
Check out the docs for GUI Text. You'll specifically be interested in the text variable of a GUI Text.
It works! Thank you! I checked the documentation but was unable to find it. A real quick question, is how would I call a specific gameobject (the GUI text) from that script attached to a different gameobject?
Yes, but you'll have to create a reference to that specific GUIText component, e.g. something like public GUIText myGUIText
in C# (`var myGUIText : GUIText` in JS) and then change the .text variable on myGUIText, like so: myGUIText.text = scoreVariable
; You'll also need to assign a reference to that script in the Inspector.
Answer by A_Devloper · Sep 16, 2014 at 07:10 AM
To find the GUIText object, you can also use GameObject.Find, like so:
guiText = GameObject.Find("GUI_TEXT_NAME").guiText;
For effieciency, do this in the Start()
function and save it to a private GUIText
variable.
As of Unity 2.0, you can also use the GUI.Label function.
Answer by Ziaxp · May 31, 2015 at 08:19 PM
First put using UnityEngine.UI; then do the following and attach this script to UI TEXT Control.
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);
}
}
I'm having an error with this one, it says "Cannot implicitly convert type 'int' to 'string'. I've copied your script exactly.
Answer by Bunny83 · Apr 06, 2011 at 01:15 AM
Like Elliot said:
guiText.text = "New Text";
You didn't tell us what language you use, but fortunately this line is the same in JS or C# (i guess you don't use Boo? right?) ;)
edit
To change the GUIText of another GameObject you can put a public variable at the top of your script and drag the desired GameObject/GUIText onto the variable in the inspector.
// JS var targetGuiText : GUIText;
function Start() { targetGuiText.text = "Hey ya!"; }
Hi. Sorry, yes I am using Javascript. Lol about Boo. I have never even heard of it before Unity. :P
Answer by Danao · Sep 16, 2014 at 07:30 AM
Here what I used. I tried a few other methods that gave me some issues, this seems to work great with the yourText GUI Text Object dragged into the inspector:
public class WhateverScript : MonoBehaviour {
public GUIText yourText;
void Update (){
yourText.text = "Coins " + currentCoins + "/" + maxCoins;
Your answer
Follow this Question
Related Questions
changing font + size of text 1 Answer
HELP with scripting where GUI is involved 2 Answers
How to make text damage notifiers above enemy 1 Answer