In this lesson, we'll add a way for our player to count the collectibles he has picked up. All right, let's move this coin a little bit, so that he needs to jump. To get it. Let's go ahead and add a text, which will display how many coins a user has picked up. To do that, go to hierarchy, click on Create UI. Text.
Ui means user interface. All right. So we see that we have Canvas, text and event system here. So the text is a child of Canva. But where is the text? We have the text new text, we don't see it.
All right, let's click on it and press F button. Oh, here it is. Alright, let's change the view a little bit. All right, where is our support man is here. Okay? The text is here.
And that's okay. We don't need to move it. In our scene, it will be appear Don't worry. Let's click on the text and change it to, for example count. Let's change its color to read. And we can also change the font size.
Let it be 20. All right, now let's change its position on canvas. To do that, find that rect transform, click here, press Shift plus out And choose any position you want. No, let's go back now let's save it and go back to our script. First of all to use user interface we need to add a new library. And I hope you remember that all libraries that we have we have up here.
So let's add a new library using unity engine.ui. Next, let's declare two variables, one count coins, another is the text. Private in the type of sent the name is come This variable will be counting our coins and another variable. Let's make it private also, but use serialized field. Alright, so private serialized field type is text and the name is count, text. Pay attention that text type is from you I library.
So if you don't use this library, this type doesn't exist. Okay, now inside the start function, we'll start with no coins. So count is equal to zero. And the text appears our text has count. txt name dot txt Use count text variable, which I have created before count, and how many coins and this number how many coins should be displayed as a string. Next, let's collect the coins and find this function where we have our checking, we add the logic for coins and for the text which displays their number.
I told you that you need to use this curly brackets if you have more than one line here. And it's even better to use them all the time. So we do it inside our F and if the tag is coin the object should be destroyed and Alright, let's save it. Alright, but before we test it, don't forget to find our script. Where is it movement script. And we have our count text here.
So let's drag and drop our text right here. All right. Let's test it. 123 Hey, that's pretty it, but I'd like to show you one bad thing we have done in our script here. We are using the same code twice. Take a look here and here.
So that's not good. The better way to fix it is to create a new function and call it just twice instead, or writing the same code again and again. The next thing that you can do is to take your camera and put it on your character. So now the camera is following our character. For example, let's make it bigger. You see the cameras following him?
Good. This is the best in first wait to follow your character by the camera. Alright, it works. Just a wait Hester