In this lecture, we'll learn how we can output text in a Unity game. And this is really useful if we want to convey information, like a score, for example, to the player. So let's go see how we can actually do that. This is the fish game we saw in the previous course. So I can drive my fish around and I can eat teddy bears. And I made those teddy bears go really slow, so I can eat them easily. What I'd like to do is add score to this game, and that means I need to do text output. Now it's fairly common to provide information to the player in something called a HUD, for Heads Up Display. So that's what I'm going to do here. But to build my HUD, I need to understand a big idea behind user interface stuff immunity. And that big idea is user interface things, like text that I'm outputting, or menu buttons that I'm going to click on, or those kinds of things go on something called a canvas. So the first thing I'm going to do here is I'm going to right-click in the hierarchy window and I'm going to add a UI component, and that UI component is a canvas. Now when I get a canvas, I also get something called an EventSystem. And we will learn about the EventSystem in the next course in the specialization. But basically, this makes it so that the Unity Engine responds to menu button clicks and so on. We don't really have to worry about that when we're outputting text, but we actually would need it for the next lecture where we do text input. Okay, I've got this canvas for the HUD and I'm going to rename it to be HUD. But that doesn't give me a text display, that just gives me the canvas. So what I want to add for text output is a text component. So I'll right-click HUD and I will create a UI element, and Text is the first one, so I've selected it. And you can see down here in the game window that I have a piece of text that's showing up in the game and I'm going to modify that some. So over here in the Inspector, you can see I can change lots of stuff, including what originally appears there, and I'll just say 0. And the font, though, I'll leave that Arial. I'll make it Bold. I'll actually make it larger. I'm going to center it, and I'm going to change its color. And over here, I'm just going to make it completely white. So I know I can type in 255s for red, green and blue, and that gives me white. And so here in the game window, my score text is now sitting in the center of the screen on top of the fish. I don't think I want it there in my game, and I will actually lock it to be in the middle of the top. And I'll still need to adjust y to say -50, and that puts it in an appropriate location. Picking where the transform goes here in top center helps keep that positioned properly when I change resolutions. Speaking of changing resolutions, my canvas has a canvas scaler. And the UI scale mode defaults to constant pixel size, and that's actually a really bad idea if you're going to let the person playing your game with the Unity player change the resolution. So I'm going to click this and change it to scale with screen size. And once you do that, you provide a reference resolution. And I use 1280 by 720 which is a 16:9 resolution. Now you can see my text looks a little small, so we'll come back to text. And while we're over here, let's actually change this to ScoreText. And we'll come over here and we'll make this a little larger. And you'll notice that that disappeared. So if that happens to you as you're trying to make your text larger, come up here to the Rect Transform, where we just changed the position in y, and change the size of the text. So you can make it wider and higher. And as you can see, I got my text back. I think I'll move that down a little bit more. There, so we have a text display that is held in a text component called ScoreText. I've already created a HUD script that I will now drag onto the HUD to add it as a component. And as you can see, it's been added. I have the serialized fields to hold that ScoreText, so I'll drag that over here. And now let's go look at how that code works. So my HUD script, here is that field I just populated. I'm also going to store the actual score, and I have a constant string that I'm going to put at the beginning of the score display. So here is another example of using a string variable, although this one is a constant, and assigning it with a string literal as its value. So you might be wondering if you haven't peeked at the bottom of the screen, you might be wondering how do we actually set what text gets displayed. And the documentation for the text class in the Unity scripting manual tells us that there is in fact a text property that we can change that is the text that will be displayed. By the way, the text class is actually in the UnityEngine.UI namespace, so I had to add a using directive for that namespace, so that I could declare this variable here of the text data type. So in my Start method here, I take that text object, and I set its text property to be ScorePrefix concatenated with whatever the score is. But score is an integer, so I need to convert it to a string, so I can do this concatenation. And I can call the ToString method on any value type to actually get its string representation. I can call ToString on reference types as well, it just provides less useful information to us. So this is building the string that will be displayed by the text component. I have a public AddPoints method where you can add points to the score. And this just adds those points to the score field, the integer, and then resets the text that gets displayed to include the new score. So if I run my game, You can see that the Start method concatenated to that score prefix with 0 from my score because that's my score right now. The last piece we need to add is when the fish eats a teddy bear, it should tell the HUD to add points to the score. So we'll do this in a couple of steps. So I'll add a comment. Now to call the method on that HUD script, we're going to need to have a reference to that HUD script. And we might as well get the reference and save it in a field, so we don't have to look it up every time the fish eats a teddy bear. So I'm going to save here a reference to the HUD. I'm also going to have a constant for how many points a bear is worth. Let's say 10. And in the start method, I will save a reference to the hud. Now, we know one of the good ways to actually find a particular game object in the scene is by tag, so I'm going to go add a HUD tag to the HUD and then we'll come back. Now that I've added a tag to the hud, we can just get a reference to the script component attached to the hud by using GameObject.FindGameObjectWithTag. I used HUD for the tag for the HUD, so now we're holding the HUD GameObject, but that's not what we want. We want the actual HUD script attached to it. So I will call GetComponent and tell it I want the HUD. So here in the Start method, I'm getting a reference to that HUD script, so that in the uncollision enter to D method, if I'm actually destroying a teddy bear here, I can call the hud.AddPoints method, with the BearPoints. So now when I run my game, I start with 0 as my score, as expected, but if I eat teddy bears, you can see that the score went up. [LAUGH] I'm horrible at eating teddy bears, but there you go. So this is an example of using a pretty standard pattern where we have a HUD to display information to the player and GameObjects can interact with the HUD to update that information. To recap, in this lecture we learned that we need a canvas if we're going to include Unity UI components in our scene. And we also learned how we can add and use a text component to provide textual information to the player.