What you will learn
In this Unity tutorial, we will learn how to effectively utilize the game engine. Over the tutorial, we will learn how to use the Unity editor interface as well as how to navigate in the scene view. From there, we will learn how to import assets and how to create prefabs. Then we will shape our level even more by adding character controllers, colliders, lights, materials and physics. We will then finish the tutorial by learning how to use scripting in order to make our level interactive.
In this lesson, we're going to learn how to display our health inside of our game using an OnGUI in a GUI box. So I just opened up lesson 27, and after every single lesson, I'm going to save out the script we just completed inside of the lesson scripts folder. So after the last lesson, we just finished creating this script. That way, you can come in and see the script as it progresses as we write it. OK, so now, let's jump back over to Mono in our character script. And we have our health being updated and printed. However, the printing is invisible to the player. So what we need to do is we need to use a function built into Unity to display our health to the player. And we can use, actually, a function called OnGUI. Now, you might be asking, where do we look up these functions? And a lot of this comes directly from the support documentation and then the scripting reference. Now, in here, there's obviously a lot of information. But you can come down to important classes and then global functions accessible. And here is where all of these functions live. So we can find the update function if you scroll all the way down here. And here it is under the overrideable functions, up here near the top. So what we're looking for is a GUI or a graphical user interface function. And we can find it down here near the bottom of OnGUI. And so this function is called for rendering and handling graphical user interface events. So this is where we want to write our GUI code. So I usually just click on the function and see how it's written. All right, so here's what essentially we need to get our GUI working. So I'm just going to copy and paste this into our script just to test it out. And this is how I usually start most of my scripts or how I started them when I was learning this language. Because, of course, it's a little bit difficult to understand if you haven't used this script language before, or used scripting in Unity. So seeing these examples and using these examples to learn is a very valid method. So I'm just going to copy that in, hit Save. And let's go back to our Unity and let it load in. And let's go to our game and hit Play. And what we should see is we should see our button here. So we can see "I am a button." And if we click, it's going to say we clicked the button. Now, of course, our health is down there quickly disappearing here. So I'm going to hit stop. And it will let me now scroll up. And it should tell me I clicked the button. But as you can see here, my health was scrolling so quickly that message was lost in the middle of the update function. OK, but we do not want a button. We want something else. We want to just draw the health. So let's go back to Mono, and let's use a different GUI. So instead of this print statement, let's just remove that. And we really don't need an if. So let's hit tab. And let's just keep this GUI button. But instead of a button, we want a box. So let's type in GUI.box. You can see here, Mono tries to finish the command for you. So here, we can see what the GUI box is looking for. So Unity engine GUI box is looking for a rectangle and then a string to pass into it. All right, so let's double-click that to do a box. And so we have a rectangle here and we have a string. So now let's click Save. And let's go back to our Unity and hit play. And you can see we have some compiler errors that we need to get fixed. So let's see here. Insert a semicolon at the end. Oh, OK. So it's expecting a semicolon, and we didn't have one. That's a very common problem with scripting. But as you can see here, Unity is very good at telling us what is wrong. So now let's hit Play. And I'm going to hit-- so here, we can see at the top left our "I am a button." Now, it's a little bit too big, and we don't want it to say "I am a button." We want it to say "Health." So let's come in here. And let's just type in "Health," which is our variable. And we want to remove all the quotes from that. And let's see here. Instead of a rectangle that starts at pixel 10, 10, and then goes 150 pixels wide and 100 pixels down, let maybe move this down a little bit. So let's maybe start at pixel 100, 100, which is 100 pixels from the top left corner. And for the width, I think 150 will be good. But let's actually just lower the height a little bit, maybe down to something like 30. And, of course, we can save this and test these values inside of our game. So now, let's jump in. Hit Play again. And we've gotten another error here. So let's see. So what this error is telling us-- let's just read it out. "No appropriate version of GUI box for the argument list rectangle, int ." In this case, integer or "int" is integer. And so what's happening is we're passing a number in-- 100-- when the GUI box is expecting us to pass in a string, like "This is a button." So to do this, let's convert our health into a string inside of this command. The easiest way to do that is to just add in a string. So to create a string, the simplest thing we need is two quotation marks. And then, let's just add a plus sign. So we're saying, take this empty string and then put this number on the end of it. Now, at the end result of all of this, you're going to have a string that simply contains our health value. So let's hit Save, and let's go back to Unity here. Give it a second to update. And we can see our update has completed. So now we can hit Play. And now, we can see inside of our viewer, that our health is appearing. Now, it's appearing without any context. It's just a number. So let's actually go back here. And instead of putting in an empty string, let's say, Player Health. And then, put a colon so that we can define what this strange number is. So let's hit save here. Let's go back to Unity. And let's-- whoops-- dragging around my console. Let's hit Play. And now, we can see our player health steadily decreasing. So now, we have inside of our interface a GUI box, or a rectangle, that stays with our player that updates the player's health on every single GUI update, or every single frame. So I just hit Control P to end that. And so now, we've successfully added a very, very rudimentary graphic user interface, or GUI, to our character state. Now, in the real world or in real programming, you would probably want to take this graphical user interface code and use it in a different area. We usually do not want to mix data processing, which is changing numbers, with the actual code that uses to display those numbers. And we're only showing it in this case because it is such a small example. Now, in a really Unity project or a real Unity game, you'd have very many scripts with very complex interactions with each other. So instead of having everything in a single script, we would want to have the character state or the game state live separately, and then have a completely different game object to handle the graphical user interface. In this case, we're just using this as a placeholder until we can get the full-fledged GUI script up and running. So GUI box is just one of the many commands we have access to. And again, the GUI area of the scripting guide will have much, much more information here. So you can see that there's many pages in the help documents that tell us how to use styles, skins, and various other parts of the built-in Unity GUI. So now that we've got the player health inside of our scene, let's now take a look at how we can only remove health when were near or inside of a fire, as opposed to the player constantly losing health while they're playing.