Building UI Elements

Ahad L. Amdani
3 min readJun 3, 2021

--

Main Menu, Score & Lives UI, flickering Game Over screen, Credits, and Restart functionality

It’s fairly straightforward to put together a working user interface system thanks to Unity’s Canvas mechanics. As you can see pictured above, you can create multiple scenes, one dedicated as your build settings root index to be your main menu, and one for the actual level/gameplay (or more if you plan on handling many levels, like we expect in Sovereignty). The various UI elements are text or images, and they are directly placed on the canvas, sized and scaled to the screen/viewport/camera.

Main Menu Scene
Game Scene

For the case of the lives, we leverage an array of Sprites that swap the source image via the UI Manager, and the Game Over screen consists of a Game Manager working in tandem with the UI Manager to acknowledge the state of a player without any lives remaining.

The UI Manager setup
General code with some variation around the game over text flickering

The return key to restart the game / load the initial scene was fairly straight forward as well.

Super simple Game Manager

Adding a user’s score was leveraged on the Enemy’s script, since that’s where we detect the collisions between lasers and enemies.

Maybe there’s a better place to put this kind of logic?

The player script itself had very little to modify in order to be able to update a score it was tracking for itself:

In the Player.cs file, we update a simple variable, referenced from the Enemy script

We had to add the player reference to the Enemy Script, though, so I may want to alter the way that behaves.

There’s got to be a better/easier way to manage cached references to important game objects, right?

In any case, this is good enough to get things functional for a prototype. Beyond this, we also need to add some basic post processing to give the game more polish and give it a feel of completeness.

--

--

Ahad L. Amdani

Father, software developer, writer and avid gamer.