5. Head-Up Display (HUD)

In this part, we will load a head-up display (HUD) on top of our game view. This HUD will later be responsible for displaying health, keys, power ups and the pause menu.

The technique we will be using for our HUD is to build it all in a separate scene, and then load that scene on top of the real game scene. This has a few advantages over using a prefab:

  • It allows us to use prefabs in the HUD (which we can't do with a prefab-HUD because Unity does not support nested prefabs)
  • It allows us to use embed graphs and scene references for our GUI elements, which greatly simplifies the Bolt workflow

The HUD is already included in the tutorial assets under Scenes / HUD



1. Temporarily disable the pause menu

Because we won't be implementing the logic for the pause menu until the end of the tutorial, we will disable it for now so it doesn't cover our view while playing.

Open the HUD scene, select the PauseMenu object, and make it inactive:

2. Create a HUD loader

Create a new flow macro called HudLoader. In its graph, we only need these units:

We use the overload for Load Scene that has a Mode parameter, and set the mode to Additive. That will tell Unity to keep the current scene, for example Level1, but also load our HUD scene on top of it.

Lastly, we'll create a simple prefab to run this loader:

  1. Open the Level1 scene. 
  2. Create a new empty game object called HudLoader
  3. Add a flow machine component to it
  4. Set its macro to the one we just created
  5. Drag the game object in the Prefabs folder to create a prefab from it

The final inspector should look like this:

3. Add the HUD loader to every scene

Open Level2, Level3 and Level4 and drag the new HudLoader prefab into each scene.

If you test your game now, you should see a row of hearts and icons appear in the top right corner when you enter play mode:

This article was helpful for 26 people. Is this article helpful for you?

I have an similae error "InvalidOperationException: Variable not found: 'Speed"

When i move to level 2, the "Object" variable Speed is "lost". I have the Flow Machine set to Macro

I did the same thing.  Go back to Level1 and select Player in the hierarchy.  In the Inspector, click "Apply" to save everything to the prefab used in Level2, Level3, etc.  Make sure you do this for the Objective, Spikes, and any other prefab you change.  If not, the variables, etc. only exist on the instance of the object you added them to which is why Player has the variable Speed in Level1 but it disappears in Level2.

The HUD Scene is loading a thousand times in the inspector after i hit play. After 10 Seconds the game begins to lag. Any Ideas?

+3

Oh never mind. I Just had the HUDLoader inside of my HUDScene .... sorry.

@Lazio The variables so far I've had trouble with are due to what @PROMC2000 mentioned, not hitting Apply on the Prefab.   By chance is there an "Apply All" Prefab button anywhere in Unity, or perhaps Unity needs to put a checkbox up there "Always Apply Changes to this Prefab" -- that would be hot.

Well done!

I was able to get through this tutorial exactly as described. No errors, warnings or extra info came up. Hearts displayed as expected.

Wot? This I had no idea about; to create a scene on top of a scene? Have I really been living under a rock this entire time?

+1

Hey there!

Thanks for all the tutorials, but I've got a weird issue (using the latest 2018.2 version).
When I'm trying to load the scene - it drops an error 

Scene 'HUD' couldn't be loaded because it has not been added to the build settings or the AssetBundle has not been loaded.

Had the same issue with the 'level2' scene load.

All the scenes are marked to be built in the build settings. I saw some comments about it in the level2 load tutorial, and the proposed solution was to manually drag the scenes into the hierarchy and rightclick -> unload them.

But if I do that (and everything will work then) and then at some point double-click to open a different scene - all of that loading-unloading in level1 will be lost, and I'll have to re-add it all again.

Hopefully someone can give it a look.

+1

Ok, seems like the isse was that the build synced incorrectly through the Unity's Collab functionality.

I removed all the scenes from "Build Settings" and added them back again and Built the project.

Works fine from there.