GameDev : Game Over Text!

Now we have little more experience with UI.
To create a Game Over Text, well, we need a Text!
On Canvas, right click and create new Text and call it Game Over Text

Change text inside to GAME OVER, and make it white in color

Now we have to adjust the size.
if we try to use a big number, like 55

The Text disappear.
This because we need to make an adjustment here in “Horizontal Overflow” and “Vertical Overflow”

Change both to “Overflow”
and use the Rect Tool to fill the Text

And move it a little to the center.
For me this is ok

We have the visual part, now we need the logic.
And that’s easy, because after setting up the score text, it works exactly the same way.
First, we need a reference to Game Over Text.
Where?
UI Manager control the UI, so open its script and we create a reference to Game Over.

And create a GameOver() method.
We just want the text to appear when we die. Nothing more, nothing less.
A good way can be to ENABLE the text when we die.
That’s all.

TIP: I absolutely didn’t know HOW, but I just googled “How to Enable Text in Unity” and found the solution.
enable is a property with get and set, so we can assign a value of true or false.
Inside the method we need to activate the Text, but at the beginning of the game we need the Text be inactive.
I prefer to do everything in code, so in Start I set
gameOverText.enable = false

When we call Game Over?
When we died, right?
Inside Player script, go to the Damage() and inside if (lives < 1) we call GameOver()

_uiManager.GameOver()

We test it

And everything works perfectly.

Just for information, I have tried to enable / disable the text directly, but we can use this syntax

gameOverText.gameObject.SetActive(true)
gameOverText.gameObject.SetActive(false)

and it is absolutely ok.

Again, there is no right or wrong, there is only a better way to make things.
And honestly I prefer this last syntax, and I change to it

P.S. I made some research about enable vs. SetActive().
I’m not sure, but probably SetActive is better when we disable the ENTIRE GAMEOBJECT, while enable is better if we need to enable / disable A COMPONENT OF AN OBJECT.

So the SetActive () solution is probably better.
But as we can see, enabling works in any case.
So, again, it’s not a wrong solution, but SetActive is A BETTER solution.
But in any case we have the right logic on Game Over Text, we need to enable it when we are dead and disable it at the start of the game.

Logic is always more important than code, without it we cannot solve any problem!

Another good solution is not Create the Game Over method, but write directly inside the UpdateLives()
We create an if statement to check

if (currentLives == 0)
gameOverText.gameObject.SetActive(true)

And again, probably this is the better solution.
we have less code, less method, when lives == 0 the Game Over Text becoming enable.

So if you want, we delete GameOver()
we move gameOverText.gameObject.SetActive(true) in the if statement,
inside Player delete the uiManager.GameOver()
and test it

And again, it works perfectly.

Always in love with programming, even if late (I'm already 39 years old) I decided to follow my dream! We will see how far my passion will take me!