Hello, guys.
In this video, we're going to work on game UI.
We're going to make it work.
We're going to update the info on coins score we need to display if we have extra life at the moment
and we want to make that pause button functional.
All right, first thing first, let's update the score and coins.
Let's go to game manager over here and make sure you have player in this field.
Just need to drag it.
Let's go inside.
Let's try to tidy up this somehow.
First I want to take this coins and move it just a bit below and add another one load type with a name
distance.
So this is going to be like header for score.
That's core in for.
Yeah, let's have this and let's make another header four colors.
We're going to have them later color info and we have platform color and we need another one.
Player of color.
Which by default going to be coloured white.
All right, now, how are we going to update coins and distance?
We are already updating coins from the coin script when we're picking it up.
If you remember, we're doing coin plus plus.
So we are good there.
But the distance, we did not do anything with it yet.
So let's make an update.
And in the update I want to check position of the player.
So it's going to work like this.
This is the x axis and distance at the moment is zero.
If player goes higher than the distance value at the moment is zero, then we change this distance value
to X of the player and if position of the player is five, then distance is going to be five as well.
And so on.
If value is bigger, then we're going to change it.
So let's do this.
If player transform position that X bigger than distance, then distance equals to player transform
position that x pretty simple.
Now let's go in the unity and create another script that is going to be UI in game.
UI in game.
We're going to use Text Match Pro in this script and usually you need to add using TM Pro for that.
But recently Visual Studio was updated.
Just I don't know if you already use an updated version or not, but at the moment if I would like to
create a variable and let's try this.
Here lies Field private Text Match Pro.
You can see if I'll click enter is going to be added automatically at the top.
It is there.
So if you have an error of this kind, just do it manually and type here using TM Pro.
Same for the UI if you're going to use UI elements like image for example.
Yeah.
Lets me do this and we need the name.
Okay.
So it's going to be distance and another one for.
Points.
Same for the UI.
As I said, if you're going to have private image which is a UI element, then it's going to be added
automatically at the top.
By the way, we need image, so let's do this serialize field private image.
That's going to be.
Part empty.
And let's just duplicate this and make hard pull.
All right.
So we have the UI elements.
We got access to them now.
We need to update them.
We could do it in the update.
But actually, I want to do something different.
I want to show you another way to update info so it doesn't take as much processing power as the update.
Even though this is a simple example and even your phone will not get hot with this code in the update.
But still it's a good practice just to know this.
In case you need to update some info someday.
And you don't want to use an update because update called like once per frame and there are 60 frames
per second or more.
And imagine how many times it's called right?
If it's more than 60 days, it can be called 100 per second.
What we can do is to create a function that I'm going to call private void update info.
Inside, I'll take my distance.
That text equals to game manager dot instance dot distance, and this distance is a float number.
So we need to convert float to string because text is a string variable and float is a different type
of variable.
Right?
So we need to do distance dot to string.
That's the same for coins.
Coins don't text equals to.
Oh, thanks for help.
And now in the start, we're going to do invoke repeat in which you're going to repeat function.
After some time, we're going to need a name of the function is going to be update info.
It's going to start at zero second and it's going to update every point 15.
Yeah, it's still a small value.
It still is going to be fast update, but not as often as the update info call.
Now let's go back to Unity.
I'm going to find my endgame UI.
And by the way, we need to switch on main menu by default.
Yeah, let's find Endgame UI and drag script over here.
Now we need to fill up this field.
Let's open this find info bar.
This could be renamed to distance.
Then in the endgame, you alleged drug distance.
Coins.
Then open extra life display.
Heart, empty.
Heart full.
All right, we're done with this.
Let's go to play mode and see if it's working.
Top to start.
Okay, we can see info is updated coins and distance as well.
Now we need to make it a bit prettier because this looks ugly.
We can make it pretty by using format.
There are different ways to do formatting for text.
You can find them in Google of course, because nobody remembers them, I guess.
But some people do.
I don't.
I always have to google this.
So what we do is just type format string C-sharp and you can see the result I did before.
And for example, I'll just type the time.
Yeah, let's open the link from the Microsoft.
And here you can find all of the different formats you can use.
For example, they explain how to do better date over here.
What I just typed today is then to string and this is the format.
And then this is the result.
Right.
And there are different ways to do format.
You can find them here.
What we're going to use is just format with one comma.
Let's go back to code and type quotation mark.
Hashtag, comma, hashtag.
And that's the same for coins as well.
Now let's go and see the result.
Now we can see that number is updated and number is pretty.
We only need to do a few adjustments here.
First one, I want to type letter M there.
So it's kind of understandable that is meters or something like that.
And another issue is when you start the game, you actually can see this score is completely empty.
Look carefully.
First seconds, there is nothing.
So that happens first because our players position is -18 at the start.
Yours might be different, but it doesn't matter because we update position when it is bigger than zero.
If you remember.
Let's double check that in the game manager over here.
We do change the distance.
If X position is bigger than distance, and distance by default is zero.
So what I want to do here is to go to ingame UI.
When a random name of this from distance to distance text.
Then here from coins to coins text.
Now let's go and create a couple of private variables.
Private.
Long distance private int coins.
Then in the update info we're going to assign those values to the one from game manager.
So distance equals to the manager dot instance the distance coins, same game manager instance coins.
Then what we're going to do here is to check if distance is bigger than zero.
Then we're going to update the text.
Let's move it here and over here we're going to remove game manager instance.
And just I want to add here, last quotation marks, Space, space and letter M.
And here we're going to check if coins is bigger than zero.
Then we're going to update coins.
Text.
And maybe we can change this value from 15 to point two.
All right, this is good.
Let's save it all and see it in the unity.
We only need to assign variables of the text one more time.
Let's choose in-game UI and drag distance and then coins.
We also need to make sure we updated the text so a distance should be zero here by default.
Just put zero space M and for the coins choose it and type zero here.
I guess I had some number here before, but I did change that when I was testing.
Now let's see.
When I go top to start, there is zero zero and once it's bigger than zero is going to change.
All right.
And it's kind of represents the reality.
Like we start counting once you jump off of the base platform.
You can see that it is cool.
I guess I like it well enough for this video.
Let's go to the next one and update the heart icon.
It's going to be pretty easy.