We need a reference of this text.
This Apple text as well as this text.
We need a reference, a reference of it.
Then we can change.
So if I move over to my hierarchy then canvas and then select this UI and then go over to this current
Apple text current Apple text.
Then go over to Inspector and then Text component.
And we can change the value over here.
We can set it to one.
We can set it to two.
We can set it to three.
So basically what we need we need to go over to this current Apple text game object first.
And then we need to access this text.
And we're going to set this text equal to our current apples we have collected so far.
So if I select my player GameObject in the hierarchy then go over to Player Collectible Script in in
in in Inspector.
As you can notice we have current apples zero and we showing up over here.
So we also need to update this in the UI.
Basically we need to set this current UI.
Basically this current Apple text with our, uh, this, uh, current apples.
So we need to do that to do to get the reference.
So let's open up our script first.
So we're going to move over to our player GameObject in the hierarchy.
And let's open up the player collectible script in the Inspector by double clicking on it.
And we will go to the top of our class.
And we're gonna, we're gonna get that text component.
And before you get the text component, make sure you just go to top of your class and include this
namespace called using Unity Engine dot UI.
So whenever you're working with UI, make sure you include this namespace.
It's called using Unity Engine UI because that text element is a UI element.
So that's why.
So we're going to make a serialized field, sorry, serialized field, uh, a private and a component
that we're looking for is a text type.
So we're going to type this text and we can call this one something like Current Apple Text.
So let's call it current Apple Text and then close that up with semicolon.
And we're going to press down Ctrl s on our keyboard in order to save our script.
And now let's move over to unity.
So underneath our player collectible script and another field just going to pop up.
And it will be current Apple Text as we name it the parameter or the variable.
So what we need we need to drag this current Apple text into that slot.
So we will have the reference.
And after that we can link that up with our current apples.
So now we can see underneath our player collectible collectible script it will be null like this.
Empty.
So what you need to do you need to drag the text component right over here.
And we do know that if we select this current Apple text then go over to Inspector.
We can see the text text component is sitting on its current Apple text game object.
So that's why we need to drag this GameObject into that field.
So we're going to select our player collectible script.
And let's select this current Apple text and then drag it over to that slot.
And now we can see we have the current Apple text reference.
Basically this text component as you can see.
And now we are ready to set that up.
So what we're going to do we're going to open up our script.
And we need to move over to in which line of code we doing.
Basically collecting our apples right over here.
You can notice we just increasing our current apples.
So we also need to update that in the UI.
So basically what we have to do we need to go over to this current Apple text game object first.
And then we need to access this text.
And we need to set this text equal to our current apples.
And basically means how many apples we have right now.
So let's go over to our script.
So after collecting the apples we're going to move over to that current Apple text game object.
And now we're going to access the text component.
So by dot text.
And we're going to set equal to our current Apple tax.
Sorry, not current Apple.
We basically need to set this current apples and close that up with semicolon.
But the problem is that this one over here, as you can see it, this is an integer value.
So if we go to top of our class we can see this current apples is a integer value.
We can notice right over here.
And this right over here is a string value.
So we need to convert this integer value to a string.
So how we can do that.
We basically need to type after current apples dot two string.
And this function just gonna convert a string value.
Sorry.
It's gonna convert integer value to a string.
Now this is a string value.
So we firstly going over to current Apple text game object.
Then we accessing the text component.
And we're setting equal to our current apples.
So let's suppose this current apple something like five.
Then this current apple text text right over here five.
Why.
Because we directly setting it as you can see.
So firstly we're just going over to this current Apple text game object.
Then we accessing this text and we're setting this text equal to our current apples.
As you can see right over here.
So that's why it's going to update in the UI.
And we're going to press down Ctrl s on our keyboard in order to save our script.
And make sure you just move over to unity and check that out.
So as soon as we're going to collect the first apple, we will be able to notice our current Apple text
gonna be one.
And if we collect second Apple, then it's gonna be two.
And so on.
So you can check with many apples you want.
I'm gonna duplicate this apple.
So I will move over to hierarchy and let's go to.
Sorry.
We, uh, have the Apple.
So if collectibles right over here.
So I'm going to select this heart and let's press down Ctrl D to duplicate.
And we're going to press down F in order to focus on it.
And let's select the move tool in the scene view.
Sorry we duplicated this heart.
So I'm going to delete that.
I'm going to select the apple in the hierarchy.
And let's press down Ctrl D to duplicate.
And let's move it over somewhere around here.
And I'm gonna duplicate one more time.
Let's move it here.
So we have three apples.
So as soon as we're gonna collect the first one our current apple is gonna be one.
And if we collect the second one, then it's gonna be two.
And if we collect the third one, then it's gonna be three.
So let's check that out.
So I'm going to click on this play button in the top in order to play our game.
And let's take a look on this.
So as soon as we're gonna collect the first one our current Apple text will be one.
And if we collect the second one then it's gonna be two.
So if we collect the first one.
So let's try to collect this.
As you can see, as soon as we collect it, this current apple takes right over here.
If I select my canvas in the hierarchy and press down F in order to focus on it, we can notice that
this current Apple tax is now one.
And if we try to collect the second one now we can see it's now two.
And if we collect the third one then it should be three.
So if we sorry if we collect the third we can see now it's became three.
So I hope you get the idea that how you can do it.
And we're gonna do the same thing as well with our heart.
So let's get out of play mode and let's do the same thing.
So firstly, what we need if we move over to our hierarchy canvas and then UI game object and select
this heart text, we can see that the text component is setting on this heart text game object.
So we need a reference of this text component.
And after that we can just link that up with our player Maxell.
So we're gonna open up our script and let's go to top of our class and let's grab the reference.
So we're gonna make another serialized field private.
And the component we're looking for is a text type.
So we're going to type this text and we're going to call this one current, uh, current, uh, let's
call it current health, uh, health text, something like that.
And we're going to close that up with semicolon and make sure once you press down Ctrl S, then move
over to unity underneath your, uh, player collectible script, you're gonna able to notice that,
uh, sorry if I select my, uh, player GameObject in the hierarchy, and I need your player collectible
script or wherever you just made it, you're gonna see that fill.
So make sure you drag the reference.
So after grabbing the reference now what we need to do, we need to set that up.
And we're going to set this up with our update function.
And also we need a reference of our player's game.
So if I select my player game object then go over to Inspector then player skip.
We can see inside this player script we have the max health.
So we need a reference of this player skip.
Then we can just grab the reference.
So make sure you just grab the reference.
And we do know that we already done it.
So if I open up my player collectible script.
As you can see, we already grabbed the reference in the start function.
As you can notice, firstly, we just created a variable for our player script.
As you can see private player player.
And then we just moving over to start function and we're just taking our player script.
As you can notice this dot game object basically referencing itself.
And we're just getting the player script component or player script and we're storing into this player
as you can notice.
So we already done it.
And now what we have to do, we need to update in the UI.
To do that we're going to move over to this update function.
So update function runs every single frame and every single frame.
We need to update the health in the UI text.
So what we're going to do now we are gonna do this thing.
Firstly we will go to current uh health text.
And then we're going to access the text.
And we're gonna set equal to our player Dot Max hell.
And we're going to close that up with semicolon.
So we're getting the same error, which is uh, this is integer value right over here.
So if I move over to my player script.
So we basically accessing this player and we sorry accessing this max cell.
And as you can notice this max cell is a int parameter.
We can see.
So so what we're doing simply over here we're just going over to their player max cell.
So this is integer value.
Now we need to convert this into a string value.
To convert that we need to type this dot to string and then make this first bracket.
So firstly we just going over to current text.
Basically we're just going over to this UI.
Then this heart text heart text game object.
And we accessing the text component in Inspector.
As you can notice.
And we setting this text component equal to our player max cell.
And let's suppose player max is something like three.
Then this right over here it's gonna be three because we directly setting it, as you can notice.
Uh, really sorry.
So firstly we going over to current health text and we accessing the text component.
And we setting equal to our player Max health.
So the text will be changed according to our player max health setting.
Since we just directly setting it or we just setting it equal to this as you can notice.
And now we're going to press down Ctrl s on our keyboard in order to save our script.
And now let's move over to unity.
So underneath our player collectible script we will able to notice another field just going to pop up
which is current uh health text.
So what we need to do there, we basically need to drag this text game object into that field.
Since the text component is sitting on inside this heart text game object.
Now we're going to select our player game object in the hierarchy.
And let's select this player script.
As you can notice underneath our player collectible script we able to notice a current health text and
it says none basically empty.
So we're going to take our health text and drag it over there.
Now we have the reference.
And now we're going to be able to check it in key.
So if I just go ahead and then click on this play button in the top, you will notice that.
So as soon as we're going to collect the this heart the first.
But we're not gonna able to do it.
Why.
Because the if the max health of this uh player game object is already five, then we're not taking.
So as you can see, as soon as the game started, we can see player Max health is five just updated
in the UI.
Why?
Because if I select my player GameObject in the hierarchy, then go over to Inspector, then player
Skip.
Because in the start of the game we setting player max which is five as you can notice.
So that's why this text right over here, it's became five because, uh, we do know that inside our
skip, we just directly setting it, as you can see right over here.
So now if I drag down a little bit, basically if I make, I make the max health of this player game
object something like four.
But before we do that, if we try to collect our heart, we not able to do it.
So because we already have the max health is five and we're checking if the max health of this player
game object is already five, then we're not collecting at all.
So let's make it something like four.
So I'm going to select my player game object in the hierarchy.
Let's go over to Inspector then player Skip and the max.
Well I'm gonna make it something like four.
Now as soon as I just type four.
It just updated in the UI because we directly setting it.
And now we will able to collect this.
So if I collect this heart, as you can see, as soon as we collect it, it disappeared.
And we can see the max health of this player game objects.
Now it's became five.
So I hope you get the idea that how you can link up your UI text with script.
So I'm gonna get out of play mode.