Hello guys.
Welcome to the video.
In this one we're going to upgrade skin system.
I want to be able to purchase skins with the fruits that I collected before during the gameplay.
So how are we going to do that.
First of all let's enable skin selection UI and inside of the skin selection, I want to display the
bank with the fruits amount and also the price of the skin above over here.
And maybe we should take these two buttons and move them down a little bit like that.
Yeah.
Like it's a bit better.
Main character.
I'm going to move it slightly to the right.
Yeah, I like it this way.
Now let's make, uh, let's take this previous skin button.
I'm going to duplicate it, then I'm going to change it to us.
Skin price.
Let's move it here.
Let's increase it to something like that.
And it would have a price of the skin, would say.
Price 25.
And we need to add the icon of the fruits probably.
That would be nice.
So let's drag the window like that to 430 by 100.
Looks good.
Let's take the text and let's align it to the left.
Maybe I'm going to reduce the size of the text box a bit over here.
Now we need an icon.
I'm going to make UI.
Actually let's make it here UI image uh, route icon.
And we need to go to project graphics items roots.
And we're going to drag this apple to the icon like so.
And let's click Set Native size.
So it's a bit bigger.
And then we can move it over here.
Something like this should work, I like it.
This will be price text.
Nice.
Now I can see it's not in the middle.
Exactly.
So I'm going to move it.
Sorry.
The parent to the left a little bit like that, I think I like it.
And we need to duplicate this to represent the bank.
So I'm going to just duplicate it.
Attach it to the top right corner, maybe a little bit down and a little bit to the left.
And let's say it has info.
Like.
Bank.
Yeah.
Now I assume that we can have much bigger bank like 683.
So if that happens, it's probably better to increase the size of this window and move fruit icon to
the right.
Yeah.
This is a bit better I like it.
Now let's, um.
Let's rename this to Bank.
Now.
Fruit bank.
I like how it sounds.
Um, fruit bank text.
And this is good enough.
Now we can work with a script.
So before we work with the script, I want to do something here.
So how are we going to do this?
We're going to make a struct.
And if you never heard of struct before I'm going to explain it.
It's really easy to understand.
Let's go here.
And we need to type public struct.
Skin.
We're going to open curly brackets, and this will allow you to create a bunch of variables under one
name.
For example, skin can have a name.
So we're going to do public string string skin name.
Then we have public string skin price.
And we want to have public bool unlocked.
Right?
So that is a three variables that we can have for the skin.
And we can have them all together under one name skin and get them from there when we need to.
Let's just go here and make it as system serializable so it is visible in the inspector.
And now if we go to the.
Uh, skin selection over here.
Let's make a header.
UI details and above.
I can make serialize field private.
Skin array.
Skins just like that.
And now let's save this and let's go back to unity and see this.
And over here in the inspector, you can see skins and you can add one of them.
And now this element has name, price and unlocked checkbox.
Let's add four more and let's fill up this data over here.
We have virtual guy with a skin price of zero and it is unlocked by default.
Then we have Ninja Frog and let's say price for this one is ten.
Then one more is mask dude.
And let's say this one is a bit more expensive, like 25.
And last one is Pink Man and this one costs 50.
All right.
So now we have an array of a skin struct.
And each of the struct has info about the skin itself like the name price.
And also you can see if it's unlocked or not.
Very nice.
Now we can go back to the script.
And before we do anything else, we want to get the amount of money and display it on the screen as
well as a price.
So we want to have a serialized field.
Private text mesh Pro GUI price, text.
We also want to have private text, Mesh Pro bank text.
And I guess it would be nice to have a reference to the price game object so we can disable it if we
don't need to buy the skin if it is available.
So I'm going to do game object price.
Game object price parent.
Let's do price parent.
For now, could we just get the reference of a parent?
I'm going to try with parent only.
Let's see.
I think it should work.
And we're going to need a reference to the text of a buy button or select button.
So we're going to do Text Mesh Pro GUI by select.
Okay.
Very good.
Now let's save this.
I can close this over here or I'll keep it open.
Maybe you need it.
And every time we update skin display.
We also want to update the price and we want to check if skin is unlocked and if it is unlocked, then
we want to remove the price GameObject and we want to update the button from the by name.
Update the button that would say not select, but by or an opposite.
So let's begin.
We're going to use this current index a lot.
And we know this is an index for the skin.
So I'm going to do maybe skin index.
Yeah.
So it's a bit shorter.
And then we're going to do this if skin.
Skins with a skin index is unlocked.
So if it's unlocked, we're going to take, um, price text.
Transform parent game object set active false so it disable entire game object.
And we're going to take buy select button text text and change it to um select.
If it is not unlocked.
Uh, else then we want to enable this game object.
So I'm going to duplicate this, move it below and do true over here.
Then we want to take price text dot text equals to skin with a skin index.
Skins I'm making a mistake again.
So it's a bad name.
Then let's change it to a skin list.
So skin least with a skin index.
Uh, dot.
Price skin price.
And let's do two strain over here.
Nice.
So this will enable the button.
It will set the price.
And also we need by select.
Text and make it equals to price.
Oh, no.
Sorry.
Buy.
Very good.
So this will update the price, the button and I guess that's it.
We also need to update money in the bank and we can go to game manager.
Let's find this safe route info method.
And we're going to take this line of code so we will not make any mistake.
Let's save this.
Let's go back to skin selection.
And over here I'm going to make a private int.
Uh, fruits in bank and it will return me that player pref.
Just like that, because I guess I'm going to need it a couple of times.
Now we have it.
We can just.
Go here.
Every time we update the screen display, we're going to do bang text.
That text equals to fruits in bank to string.
All right, I love it.
Let's save this.
Let's go back to unity.
Oh, no.
We lost it because I changed the name.
This is so bad.
Let me try again.
And the third one.
And I'm going to set Mask Dude as unlocked just so we can see and be sure it is working.
And Pink Man going to be skin price 50.
And that's good.
And we need to assign variables over here.
So let's go ahead and find uh select button and give it a text.
Over here it should be named as select button text.
Okay.
So we have select button text.
Then we need price text.
Rice text and bank text.
This is back.
Bank is over here.
Good.
Now let's go ahead to the play mode.
Okay, we obviously need to update screen display at the start, but that's okay for now.
Are we going to do that later?
Let's click the arrow.
Okay, I forgot to type words in here, but other than that, we can buy.
This is available.
We don't need to buy.
Button disappeared and again we need to buy this.
And again we need to buy this.
All right.
This is working I love it.
Let's go back and quickly fix the text over here before I forgot.
We're going to need to type here.
Bank.
Can add in space and do plus over here.
And then we do need to string here.
And over here where we type price we're going to need to type.
Price like that.
And then we do need to stream.
And of course I forgot to put a plus over here like that.
Good.
Now we want to buy the skin.
We want to unlock it.
So let's go ahead and make a method.
Private void by skin.
We're going to pass in skin.
Um, I'm going to just pass the index here.
Maybe.
Now when we buy the skin we want to take skin list skin index unlocked.
True.
And also we want to save information about this skin.
I'm going to do string.
It's in name equals to skin list with a skin index skin name.
And then I'm going to do playerprefs.
Set.
Int.
Of.
Uh, skin name plus unlocked to one.
So this will save the information when we buy the skin.
Right?
This is good.
For now, let's go ahead to the.
Select skin over here and we're going to do this if skin list.
In the skin index.
Unlocked equals to false.
We're going to type by skin with a skin index.
Else we're just going to select the skin same way as we did before.
And when we start the game, we want to check if we unlocked the skin before.
So I'm going to do this.
Let's open start method and let's do private void load skin unlocks.
Inside of this method we're going to do for loop with the skin list dot length.
And we want to get the name of the skin and check it with the.
While you're in a playerprefs.
So we're going to do bool skin unlocked equals to Playerprefs get int of uh.
We need a name.
Uh, let me just go here and quickly check.
How did we write it like that?
I'm just going to copy it.
I'm going to paste it in here.
We're going to change it to get int and it will give us zero by default.
Now we need to get the name of the skin.
I'm just going to type here.
String skin name equals to skin list dot skin name.
Very good.
And now we want to check if it's equals to one.
And if it is equals to one then it is unlocked.
And we can just do this if skin unlocked.
Then skin list with the eye unlocked.
True.
And also we want to unlock the first skin in the list no matter what.
So I'm going to do or I is equals to zero.
If that's the case we're going to unlock the first one as well.
Very nice.
Now let's go to the start method.
Over here we're going to load skin unlocks and we're going to update skin display.
Perfect.
Let's save this.
Let's go back to unity and let's see how it works.
And then we only need to, uh, take money from the bank when we buy in it.
Okay.
Very good.
I have 19 fruits in the bank.
And I can afford to buy this skin.
I'm going to click buy.
I didn't lock it.
I can see it.
I did not select it, but it's okay.
Now let me go out of the play mode.
Go to play more the game.
And this is unlocked and this one is unlocked as well.
Very good, very nice.
Now let's go to this select button.
Over here and I'm going to remove this method.
Start new game for now because we don't need it.
Oh.
Skin selection UI.
Skin selection.
Select skin.
Okay, cool.
Now let's go ahead and subtract the money.
Subtract.
Yeah.
So when we bind the skin, we want to be sure we have enough money for that skin.
I'm going to do private bool.
Have enough fruits.
And here we want to pass int price.
If fruits and bank.
Is bigger than the price, then we can return true.
And if no, we can return false.
And over here we can do something else once we check the price.
Because if we can afford it, we can do subtraction at the same time and also save the new value.
So this is how we're going to do this.
We're going to do playerprefs.
Set end.
And I'm going to take this name over here, paste it in here.
And as a value that we save and I'm going to just pass fruits in bank.
Minus the price, so we're going to deduct it.
At the same time we're going to check if we have enough.
If we have enough, we're going to deduct it.
And we're going to return true.
So we can buy it and save the new amount of money we've got.
And also.
Yeah, I think this should be okay.
Now let's see this.
Let's go to the Bioskin and do this.
If.
Have enough fruits.
For the Or we need to pass skin list.
With the index.
Dot price.
Yeah.
So if you have enough fruits equals to false, then we're going to return from here.
Why do I see an error?
Wait, what do you mean?
String should be D-Day.
Oh my God, this should be an integer.
I'm so sorry.
Int okay.
Let's go back.
Okay.
This should be it.
Over here.
I'm so bad.
I'm sorry.
Now let's go back.
Over here.
This works good.
So if you don't have enough fruits for the skin, we return from here and nothing works and we can maybe
just do a debug message.
The backlog.
Not not enough fruits and later on you can play some sound effect that sort of says that you cannot
buy it.
Yeah, this should be good enough.
Now, at any time we select the skin, we want to update skin display just in case.
So we're going to do update skin display over here.
And this should be it I believe.
Now let's go ahead and try this.
I'm going to do file three.
Edit clear all playerprefs.
Yes.
Clean them.
Now over here, nothing is unlocked.
The price is zero.
Then.
2550.
Gonna go to play mode.
We have this skin unlocked.
Bank is zero, so I guess I need to go and farm some fruits, but I'm not going to waste my time on
that.
I'm just going to go to asset in level one.
Save this.
I'm going to go to play mode.
And on the game manager.
I'm going to set fruits collected to, uh, 53.
I'm going to go ahead, finish the level.
It's going to save the info and we can go back to main menu.
Um.
Main menu.
Go to play mode.
So we have 54.
This one is unlocked, this one is not.
And this one and this one is not.
So let me try to buy one of them.
Buy.
Now I can select it and yeah okay.
This is what we need.
I believe select.
And then it's supposed to transfer me to the game or to the level selection.
That's what we're going to do.
Okay, I love it, love it so much.
I'm going to see if I can somehow clean this up quickly.
I don't know, man.
Everything looks good to me.
I'm not sure we need to do anything else here, so that's probably good.
If I find any bugs, I'm going to tell you in the end of this section.
As of now, let's jump to the next video.