How do I draw text to the screen?

So I just discovered that the method I’ve been using to draw text to the screen is actually creating a space the user can edit. (GUI.TextArea.) I’ve actually been calling this through a custom function I wrote ages ago, and since my game never uses the mouse I never noticed you could click on these text fields. (Or if I did I decided it was a non-issue since the game wasn’t going to use the mouse.)

But I just noticed this is a problem when I hit the “tab” key, which I tried to bind to opening a special menu. “Tab” cycles through all those text fields on the screen. Every single one, even though they aren’t supposed to be editable text.
And most of the things on my HUD and all of my menus… They all display text so I’ve been using the wrong function to display text.

So… What am I supposed to use to display text? How man I draw text on my screen as a static element instead of something the user can edit?
I’m looking through the GUI in the scripting API and I don’t see any other methods for displaying text.

Or failing that, is there a way to disable the “tab” key from selecting text fields, so that I can use it as a regular button even when I have text on the screen?

2 Likes

Oh man I can’t begin to express how pleased I am at such a fast response!
Thank you, thank you very much!

GUI.Label should really only be used for custom editors and propertydrawers, not for game text.

You would be better off looking into UnityEngine.UI canvases and the TextMeshPro text elements for game UI text.

1 Like

This ^^^^

GUI.Label is the “correct” way to do it if your project is stuck on Unity 4.5.x or earlier. But if you have a Unity version from the past half decade or so there is the current UI system. See UI.Text. Anything GUI.x is now referred to as the IMGUI and is no longer intended (nor tested) for use as a player facing UI. Here’s what Unity says about it:

https://docs.unity3d.com/Manual/GUIScriptingGuide.html

https://docs.unity3d.com/2017.3/Documentation/ScriptReference/UI.Text-text.html

As already mentioned there is also the free TextMeshPro package, which in some ways is superior to the current UI system.

1 Like