Time for action – setting up a GUI Text object
The Unity engine features a component/object type named GUIText
, which can display raw text and numerical values as stylized text on your game's viewport. We can write a script for a new GUIText
component that will determine the total number of coins collected and update the displayed text with that count. Perform the following steps to set up GUIText
:
Make a new
GUIText
object from the Create menu of your Hierarchy window.You'll notice that the
GUIText
position is set to0.5
,0.5
,0
by default. However, it displays in the middle of the game window regardless of your position. That's because aGUIText
position value doesn't affect its global position, only its location on the screen.We want
GUIText
to appear in the upper-right corner so that it doesn't obscure the center of our view, so we'll change the coordinates to the maximum X and Y values. The Z value ofGUIText
represents the depth, which won't become apparent until you have multiple texts...