Ah, that 80’s nostalgia. It seems to be everywhere these days, so why not? Why shouldn’t we include some of it in our game? When I say some, I mean a lot. It’s just good fun.
Using what we covered in basic UI creation, let’s make a retro-styled Game Over title.
Integrate UI elements into our game and create some blinking text.
I will begin by creating an empty GameObject that I will rename to UIManager. This object will hold our UIManager script that will handle the user interface events.
Next, I will create my Text Element in my Canvas. I changed the color, scale, and position to match my idea of the result.
I proceed to create my UIManager script. Inside of this script, the first and most important thing to add is the using directive for
Note: A Using Directive allows the use of Types in a Namespace so that you do not have to qualify the use of a Type in that namespace
Our first variable will be the one that holds a reference to our Text object. I then make sure that the Text Component is disabled at Start.
The line you see above uses
GameOverTxt.enabled = false . This method allows you to turn single Components on a GameObject on or off.
We can now drag our Text GameObject directly into the variable field of our UIManager Script.
Great! So what do we do with it now? Ultimately, my goal is to have this Text appear when the player dies. And once it appears on the screen, I want it to blink on and off. This behavior will give the game some of that retro flavor I am after.
Let’s start by creating that behavior in the UIManager script:
I began by creating two variables. The
bool named GameIsOver will determine how long the effect lasts. The
float BlinkRate will control the speed of the behavior.
The Coroutine is very simple yet effective. It enables the Text Component, pauses execution for a set amount of time, and disables the component again. The Component remains disabled for the same amount of time, and the loop starts all over again.
Let’s test it out.
Hawt dang! The speed of the blinking feels good too. The final step is to use Script Communication to have my player inform the UIManager when it dies. Before I do, I will create a public method in my UIManager to start my Coroutine.
This new method will set the value of GameIsOver to true and will start my Coroutine.
Back in my Player Script, I can modify the
TakeDamage() function to control the required behavior. But first, I have to create a variable of Type UIManager so that I can communicate with it.
Finally, I will add the call to the
OnPlayerDeath() method in
All done! When my player’s lives reach zero, it will first call the UIManager and then destroy itself.
The final result:
With the help of Coroutines, I have created new behavior to enrich my game. I can still do more to this Text to improve on the final product. I could have it change colors or gently fade in and out. The possibilities are truly endless. Sometimes, this is the real problem.