- Home /
GUIText doesn't update
Hi. I am using GUIText to store and render a simple timer. I am setting the content thus:
guiText.text = "Timer:" + Global.timer;
However when Global.timer is decremented the view on the screen doesn't auto-update. The view is updated using GUI.Label (below). However I have opted for using GUIText for ease of font manipulation. I could stay with Label but for being able to assign a font (.ttf asset import) to it, in a Javascript.
I can manually update the GUIText content of course using the above but that screws with the font i.e. the global score variable doesn't seem to be the same font as "Timer". Any ideas how to solve either please?
Thanks
G.
GUI.Label (Rect (300, 25, 100, 30), "Timer: " + Global.timer);
Answer by Eric5h5 · May 19, 2012 at 06:33 AM
You have to use Update; the text won't change once it's been assigned unless you change it. Also, you can use whatever TTF font you like in OnGUI code.
Thanks. Sorry, I am in the doco (newb) and I don't see any reference to Update. What is 'Update'? A method? Object?
Thanks
Please don't post comments as answers. Anyway, just use the search box in the scripting docs.
If you don't know what a method or an object is, you need to take basic program$$anonymous$$g classes.
Syclamoth, to extrapolate I was asking if Update in this context is a method or an object.
I see Update is not a method of GUItext nor is it in the inheritance hierarchy. I can't find it as an Object which leaves only the Update function called in each JS attached to a GameObject. There are 2 problems with this, assu$$anonymous$$g this is what you ere referring to, firstly the string is still rendered using 2 different fonts and second the Update function is being called x times per second resulting in a massive, unnecessary overhead.
Thanks for any further clarity.
@garybaldi688: This is again not an answer to the question. Answers should answer the question.
A guitext component redraws itself (via the guilayer component on the camera). The text on the other hand doesn't update itself. Everytime you change your Global.timer variable you have to execute this line again:
guiText.text = "Timer:" + Global.timer;
If you use GUI.Label it has to be used in OnGUI which is also called every frame.
You can use a custom GUIStyle or a GUISkin (which is just a collection of GUIStyles) to change the font of a certain GUI element.
Answer by garybaldi688 · May 20, 2012 at 10:17 AM
Ok this fixed the issue:
var font : Font; guiText.font = font;
Adding this before I manually update the guiText now sees the text nicely formatted, the same font etc. This makes no sense.
Your answer

Follow this Question
Related Questions
How do adjust GUI.Label lineHeight? 1 Answer
Update Guitext every frame 1 Answer
GUIText not updating score.... 1 Answer
Updating an interger in a GUIText 1 Answer
Timer lag at GUIText drawing. 1 Answer