Create the Text message
In this example, highlight “Canvas” in the Hierarchy, and pick UI — Text from the right click menus.
Create new Text object to pop up
(a) name new text object,
(b) rename the text message. Here we use the text “GAME OVER.”
(c) color object.
Sizing Big
After entering a large Text — Character — Font Size, the message disappears on the Scene and Game views. This is because of the properties of “Horizontal Overflow” and “Vertical Overflow.” Large Font size overflows the default choices “Wrap” and “Truncate.”
Horizontal and Vertical Overflow default settings
Choose “Overflow” on both settings.
Overflow Settings
In the Inspector, update the property “Text — Text” from “New Text” to the final “GAME OVER”:
updated object in the Canvas collection, with new “GAME OVER” text
Adding the Code to Display the Text
Add the variable to reference object Game_Over_text. There are two choices, type “Text” and type “GameObject.” As the desired method is “SetActive()” which is a method of GameObject, choose type GameObject.
public class UIManager : MonoBehaviour
{
[SerializeField]
private GameObject _gameOverText;
...
}
Save and look for variable to appear in UIManager (Script).
Variable “_GameOverText” unassigned as “None (Game Object)”
There is some back and forth here. The variable was coded in the class UIManager. The object Game_Over_text is a child of the container Canvas. The component “UI Manager (Script)” is listed in Canvas, but not the three children.
If “None (Text)” appears — type “Text” appears because “private Text _GameOverText” was entered in the C# script.
If “None (Game Object)” appears — type “Game Object” appears because “private GameObject _GameOverText” was entered in the C# script.
Drag and drop the Text object “Game_Over_text” into the box next to variable “Game Over Text”:
“Game_Over_text” assigned to variable
The text-now-a-gameObject can be turned off at the game Start, and turned on with a public method by some outside class.
public class UIManager : MonoBehaviour
{
[SerializeField]
private GameObject _gameOverText;
...
void Start()
{
_gameOverText.SetActive(false);
...
}
...
public void ShowGameOver()
{
_gameOverText.SetActive(true);
}
}
Access from outside the UIManager class uses the ShowGameOver() method:
public class Player : MonoBehaviour
{
...
private UIManager _uIManager;
void Start()
{
...
_uIManager = GameObject.Find("Canvas").GetComponent();
if ( _uIManager == null )
{
Debug.LogError("Canvas.UI_Manager is missing.");
}
}
...
public void Damage()
{
...
if (_lives < 1)
{
...
_uIManager.ShowGameOver();
}
}
}
Corrections from the Online Training
To avoid seeing the “GAME OVER” text before running the game, highlight the “Game_Over_text” object in the Hierarch, and uncheck the object’s box in the Inspector.
Unchecked Game_Over_text Object
Don’t remove the SetActive(false) method in the Start().
Game_Over_text as a type “Text” Object:
The advantage of using type Text allow for use of behavior methods to manipulate the “GAME OVER” text.
public class UIManager : MonoBehaviour
{
...
[SerializeField]
private Text _gameOverText;
Update the Inspector with the Canvas — Game_Over_text drag and drop to the Inspector — UI Manager (Script — Game Over Text reference:
Game_Over_text (Text) assigned reference
“child_something.gameObject.top_method()” is helpful syntax:
public class UIManager : MonoBehaviour
{
...
void Start()
{
...
_gameOverText.gameObject.SetActive(false);
}
...
public void ShowGameOver()
{
_gameOverText.gameObject.SetActive(true);
}
}