Unity Fundamentals — Flashing Text and Load Scene

Jose Gil
3 min readApr 18, 2021

Do you remember the old games that flashed “GAME OVER” in large text on the screen when you lost the Game? This article will cover how to create them, make it flash, and even let your player restart it.

GAME OVER Text

The GAME OVER text is simply a Text UI element that you create within the Hierarchy.

Once you create the Text UI Element, you can then format it as per the image below within the Inspector. Rename it to Game_Over.

Position and lock the text in the centre of the screen.

Create another Text UI Element to advise the player to restart the game.

Create a script called “UIManager” and assign it to the Canvas GameObject. Within the script, add to Text Variables for each of these Text UI Elements. Make sure you serialize.

[SerializeField]
private Text _gameoverText;
[SerializeField]
private Text _restartGameText;

Back in Unity, assign the two text Elements to the Canvas UI Manager Script.

Let’s Make the GAME OVER flash

Within the UIManager script, make sure that both Text UI elements are not active in the Start method.

void Start()
{
_gameoverText.gameObject.SetActive(false);
_restartGameText.gameObject.SetActive(false);
}

Create a new private method to activate the text. This method will need to be called by the event that ends the games (e.g. out of lives). Here we are activating the Text UI elements and starting the Coroutine to flash the text.

private void GameOverSequence()
{
_gameoverText.gameObject.SetActive(true);
_restartGameText.gameObject.SetActive(true);
StartCoroutine(GameOver());
}

Create the Coroutine to flash”Game Over” text that the previous method is calling. While true, the text will be turned on and off every 0.5 seconds.

IEnumerator GameOver()
{
while (true)
{
_gameoverText.gameObject.SetActive(true);
yield return new WaitForSeconds(0.5f);
_gameoverText.gameObject.SetActive(false);
yield return new WaitForSeconds(0.5f);
}
}

Restarting the Game

The final step is to allow the player to restart the game by pressing the R key. We start by creating a new script called GameManager as it is best practice to put these types of actions in a separate script.

Within the script, create a bool variable to specify if the game is over.

private bool _isGameOver;

Now let’s create the public method to advise the script that the game is over.

public void GameOver()
{
_isGameOver = true;
}

Within the GameManager script’s Update method, we add the conditional statement to restart the scene using the SceneManager.LoadScene command. The 0 is the index of the scene to load. You can also use the Scene name instead of the scene index.

private void Update()
{
if (Input.GetKeyDown(KeyCode.R) && _isGameOver == true)
{
SceneManager.LoadScene(0);
}
}

Return to UIManager script and get access to the GameManager script in the Start Method. Make sure you null check for debugging.

_gameManager=GameObject.Find(“GameManager”).GetComponent<GameManager>();
if (_gameManager == null)
{
Debug.LogError(“GameManager is Null”);
}

Finally, call the GameOver method from the GameManager script in the GameOverSequence method.

private void GameOverSequence()
{
_gameoverText.gameObject.SetActive(true);
_restartGameText.gameObject.SetActive(true);
_gameManager.GameOver();
StartCoroutine(GameOver());
}

You can now test your work. Good luck.

--

--