We need to show up.
How many coins we collected so far?
So we're going to create a coin UI.
So we're going to move over to our canvas in the hierarchy.
So let's select the canvas.
And we're going to press down F in order to focus on that.
So to create an icon first.
So let's do that.
So we're going to move over to this canvas and then UI GameObject.
Let's right click on this UI GameObject.
Let's go to UI.
And then we're going to choose this icon image.
And we're going to call this one to something like coin Sprite.
So let's call it coin Sprite and make sure you just go to Image Component and then Inspector.
And we need to replace this source image.
And it says sprite.
So we're going to replace with our coin sprite.
So let's move to project tab then assets.
And then I'm going to move over to my this UI folder.
And I can see I have the coin sprite.
So I'm going to select my first one.
So let's select that and drag it over to this source image field.
As soon as we drag it now we can see our coin sprite.
Uh, coin sprite.
And instead of saying coin sprite we're going to say something like icon.
So let's call it coin coin icon.
So basically this is going to be the icon.
So that's why and we're going to move it uh to top left corner.
So let's select the coin icon and let's go to Inspector Rect Transform.
Click over here.
And then we're going to hold down alt and select this top left one.
As soon as we have chosen.
Now it's gets over to top left.
And I'm going to move it somewhere over here.
And this seems pretty much fine by now as you can see.
And we need to create a text over here in order to reference how many coins we collected so far.
So it's we also need to show that up in UI.
So I'm going to select my this text right over here.
As you can see this text.
And I'm going to press down control D in order to duplicate that.
So I'm going to press down Ctrl D.
And after duplicating.
So let's select the right tool from here.
And I'm going to move it down somewhere over here I guess uh this seems pretty much fine by now.
And you can notice over here and instead of saying text, I'm going to say something like, uh, coin
current coin text.
So let's call it current uh, current coin text or txt for text.
And this is pretty much fine by now.
And we're going to move over to the parent game object.
So in our case UI let's go to overrides.
And then we're going to click on this apply all.
So the changes we made it's going to be saved into this UI GameObject prefab as well.
So if we move over to current coin text then text component in Inspector we can change the value over
here.
Something like we can set it to one.
We can set it to three.
We can set it to uh four as well.
So what we basically need we need a reference of this text component of this current coin text, then
we can just link up this text with our current coin or how many coins we collected so far.
So by default, I'm gonna set it to zero.
And let's open up the script where we're collecting.
So I'm gonna open up I'm going to select my player GameObject in the hierarchy.
And let's open up the player script where we're collecting.
So if we just scroll down and I use this coin over here, as you can see.
So we increasing our current coin by one.
As you can see then playing our coin collected animation clip.
And after that we just destroying our coin right away.
So we also need to show this up in UI.
So let's go to the top of our class and let's create a text component.
So basically what we need we need a reference of this current coin text.
Basically this text component.
And the inspector we can see.
So we need a reference of this.
Then we can link up with our current coin text.
So let's go to script.
And I'm gonna move over to top of over here.
So let's make a serialized film.
So we're going to make a serialized film, and that will be private.
And we're going to make it.
That component we're looking for is a text type.
So make sure you before you type the component name, just go to the top of your class and include this
namespace and call using Unityengine dot UI.
It's going to allow us in order to use the UI elements, because we do know this text component is a
UI element.
So that's why we need to include this namespace in top of our class, which is using Unityengine dot
UI.
So now we're going to grab the reference and we have to name the component or the reference what we
want to name it.
So I'm going to call it something like current context as I name it in Inspector.
So I'm going to call it current context.
And let's press down Ctrl S to save our script.
And let's move to unity.
So underneath our player we're going to able to see another text component just gonna appear and we
just name it current coin text.
So what we basically have to do there, we need to drag this text component into that field.
So just wait for it to compile.
And once it's done we are ready to do that.
So just wait for to compile.
And now if I move to hierarchy uh, then uh player uh game object underneath our player skip, we can
see current coin text and it says none text basically empty.
So what we're gonna do, we're gonna select this current coin text object and let's drag it there.
And we do know inside this current coin text we have the text component.
So that's why we need to drag this game object into that slot.
And now we can see we have the current coin text.
As you can see underneath our player skip.
Basically we have this reference now of this text component.
And now we are ready to set our link up with our current coin.
How many coins we collected so far.
So let's go to our script.
And over here.
over here.
We're doing it.
So let's go to heart.
So as you can see, we what we're doing basically we're just increasing our max health by one.
So after increasing sorry not Max I'm really apologize.
Basically increasing our current coin by one as you can see.
And after that what we need to do we need to update the text as well.
So what we will do, we will uh, move over to current coin text.
And then I'm gonna access the text and we will set equal to current coin how many we collected.
And let's close that up with semicolon.
So as you can see this current coin is a integer value.
If you if we move over to top of our class we can see over here we just created that parameter current
coin.
And that is a int parameter.
And this text right over here is a string parameter.
And this is string.
And this is a integer value.
So what we basically have to do we need to convert this integer this one which is current coin into
a string value.
So in order to convert that, we need to use these two string dot two string and make the make this
first bracket.
And now we're going to close that up with semicolon.
And let's move over to unity.
So what we basically doing.
We're just going over to current context dot x.
And we're setting equal to current coin.
So let's suppose current coin we collected one.
So as we collide with the first coin we're going to collect that.
So current coin is going to be one if we collect.
So the current coin over here just going to be one.
So that's why this current coin takes dot x right over here.
It's just going to be one because we directly sending it as you can see simply we're just moving over
to this current coin and then accessing this accessing this text.
As you can see in the inspector.
And we're setting this text equal to our current coin.
And let's suppose current coin right over here as one.
So this current coin text dot text is just going to be one right away because we directly setting it.
So this text takes right over here.
This is going to be, uh, just going to be one according to how many coins current coin we collected
so far.
Other coins we collected so far.
So so let's have a look on this.
So I'm going to click on this play button in the top.
And you will see as soon as we're going to collect that coin this is spinning coin.
You will see the text will also be will change because after collecting that we also just uh updating
in the text as well.
So just wait for, to collect this, uh, current coin.
And as soon as we collect it, as you can see current coin we have now one.
So if I just click on this pause button in the top in order to pause my game and move over to my player
GameObject in the hierarchy, then player skip, we not able to see the current coin because we need
to move over to debug mode.
So I'm going to move over to top right three dots.
So let's click there and let's click on this debug.
And as soon as we have chosen debug we can see current coin is now one in the bottom.
So that's is why this takes right over here.
It also changes with the current coin which is one.
So the text right over here just uh, gets one as well.
And uh, as well, basically if we move over to UI GameObject underneath our canvas, then current coin
text, we can see, uh, if we scroll down, we can see the text over here becomes as one.
So basically that is the thing.
So I'm going to get out of play mode.
And I'm going to move over to normal mode.
And in order to go normal mode, make sure you just go one more time to the top right and click on this
three dots.
And then click on this normal mode in order to go back in normal mode.
So basically that is the thing.
And we're going to select our player GameObject in the hierarchy.
Let's go to overrides and we're going to click on Apply All.
So the changes we made over here it's going to be saved into this player GameObject prefab as well.
So basically that is the thing.
So I hope you get the idea that how you can just use the text in order to in order to link up with something
that you really want to link with.
So it's totally up to you basically.
And if you just have don't want to duplicate, then you can just go over to this UI game object or simply
this canvas as well.
So we created the UI, a game object, in order to keep all of the things in an organized way.
And we just made it the size as our canvas.
So you can just simply right click on this UI game object and then move over to UI.
And in the bottom you can see legacy and then choose this text.
If you just want to current want to create the current context instead of duplicating.
Or you can simply select this text underneath your UI game object underneath canvas, and then you can
just press down control D in order to duplicate that.
And you can just rename that to something else.
I hope you get the idea that how you can do this, and I'm going to move over to UI overrides and then
apply all.