GameDev : Restart the Game (Part I)!

We set up many things till now, but when we die we can’t press Play over and over, we need a method to restart the Game.
The best way is create a new Text under our Canvas, “Restart Text” and said to Player :
“Ehi, press this Key to Restart!”

And we write a message

Now, we position the Text

Set the color to white and resize it to the size you want

This is good for me

Now: this text must appear at the same time as the Game Over text and if we press “R” the level reloads so that we can play again.

First, we need the reference to Restart Text

In Start we set enable = false

And we call it here

Drag and drop it in the Inspector and test it

We have the Text in right place.
Always we make test step by step so if something go wrong, we have a little problem to solve.

The logic is:
after we are dead, the Game Over routine starts, enable the Restart text and if we press R, reload the level.

So, first of all, create a method for Game Over Sequence.
This is because it is always better to have ONLY ONE METHOD FOR ONE THING.
This is one of the cardinal points in Unity, and it is called SINGLE RESPONSIBILITY PRINCIPLE.

There are FIVE of these BASIC PRINCIPLES, I don’t talk about them right now but in the future I will create more in-depth articles because I think it is INCREDIBLE IMPORTANT to become a better Software Engineer and Game Dev.

There is a shortcut to create a method.
Select all the line of code and press ALT+ENTER

Extract Method, press Enter again and name it

Or you prefer you can create a new method and cut and paste what you need.

We have now our Game Over method, but we need to implement the “R” key press system to reload the scene.

In the next article we implement it!

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!