- Home /
Live GUI text readout?
So basically there are two parts to this problem. Firstly, I'm trying to use unity's new (free) 2d support to make a GUI text object that will read out a number for testing purposes. Secondly the number I'm trying to display it with is currently being pulled from another class.
I'm just getting an error stating that "Assets/Scripts/FuelLSreadout.js(9,1): BCE0053: Property 'UnityEngine.Component.guiText' is read only."
does that mean its impossible to get a live readout? Heres my code.
#pragma strict
public var fuel = 300.0f;
public var lifeSupport = 1000.0f;
function Start () {
}
function Update () {
guiText = "Life Support: " + lifeSupport + " | Fuel: " + fuel;
}
Answer by tyrike thompson · Dec 04, 2013 at 12:04 AM
You should do like so..
#pragma strict
public var fuel = 300.0f;
public var lifeSupport = 1000.0f;
public var (Whatever you wanna name it) : GUIText;
function Start () {
}
function Update () {
(Whatever you wanna name it).text = "Life Support: " + lifeSupport + " | Fuel: " + fuel;
}
Answer by RyanZimmerman87 · Dec 04, 2013 at 12:16 AM
I've had GUI issues before where the text does not display correctly unless it is updated inside OnGUI()
So for example you might want:
string guiText;
void OnGUI()
{
guiText = "Life Support: " + lifeSupport + + " | Fuel: " + fuel;
}
I have no idea why this is the case, maybe I did something wrong. But I have at least several GUI displays in my project that do not work correctly if their info is update from inside Update().
Worst comes to worst you could try doing the update like in my example inside OnGUI();
Also your example doesn't show how you're trying to retrieve the data from the other script?
Answer by Eric5h5 · Dec 04, 2013 at 12:35 AM
guiText is the component; you need to use the text property.
guiText.text = "xxx";
As a side note, this is not related to the new 2D functionality in any way. GUIText objects have been around since Unity 1.
Your answer
![](https://koobas.hobune.stream/wayback/20220613123349im_/https://answers.unity.com/themes/thub/images/avi.jpg)