Welcome back my fellow game developers
to a brand new video.
And in this one, we pick up
a couple of coins, we get hit.
So our life is 200 over 300.
But thankfully, we have this health in here so I
can buy a couple of health and upgrade my health,
but not everything works.
So you can see right here
because this is something different.
This is not a half upgrade,
I mean, a health restoration item.
This one is, and I buy.
But finally you can see that I have 0 amounts of money.
So if I try to buy more, unfortunately,
I can't blame it on the economy,
blame it on whoever you want.
Let's not waste any more time.
Let's get started.
Oh, case.
So now it's time to start actually buying the items.
And the first thing that we want to do
is we want to have these texts in
here appear when the player is standing
inside of the box collider right here.
And if he's standing outside that box collider,
we don't want to see the text and we can not buy.
So the first thing that we're going to
do is I think I want to increase this a little bit.
So we'll make the size a bit bigger from this.
Oops.
One second. Increase the box colliders on all sides,
make them a bit bigger and maybe even make
the box collider that isn't a trigger,
which is this one,
and maybe just covered
the entire surface of this whatever it is,
platform with a little plus green plus,
and anyways, with that,
out of the way, I'm going to do this
off-screen is pretty easy, nothing too much.
And it always comes back to you how big,
how small you want to make it.
But there is something that is not up
to you and that is the challenge today.
We want the text to appear when we get close.
So get a reference to the byte text.
Use the entrepreneur enter and exit to set
the text active to true and false respectively.
And also create a bool that checks true when the player
is in the players on and false when he's out of it.
So that although the way pause the video
right now and go to reach shallow.
O K. Welcome back.
So I was just finishing up with these first thing I'll do
is select these three items and make them the pickups?
Yes. Make the children also pickups.
So now we are going to open up the shop items,
a script, I believe it's opening here,
and we are going to stop.
So first of all, I'm going to
create an aunt sugar enter and exit.
So on, Trigger Enter 2D and on sugar exit 2D.
Now we need a reference to the bye message.
So in here I'm going to create
a serialized field of type text Mesh Pro.
Or you know, what maybe what we can do is we can
go ahead and just change the entire items.
Or what I mean is turn off
the whole canvas and turn it on.
So we get the whole canvas,
we can turn it on and off.
So maybe that's the better way
we can create a reference to the canvas.
And here, we can create a reference to a game object.
It really doesn't matter.
But in any case, the Canvas message,
let's call this the Canvas message.
And now when we enter,
because we just have the pickup and the pick up pickups
only interact with the players of
this will not be a problem.
So in here, the bye message,
out, Sorry, bye message.
I'm still thinking of a different variable.
So the Canvas message dot set
active or dot game object dot set active, true.
And then I'm going to duplicate it.
And in here when we exit,
it's false. Save that.
Then we also need a variable which is going to be
a private bool in by zone.
And in here we are going to say that in by Zone equals
true and by zone equals false.
And maybe we instantiated it right here as false.
So we start with false.
We don't want to be able to
buy when we're standing very far away.
Okay, Great With that out of the way.
Let's go in here.
And let's select these three canvases.
So 123, turn them off,
run the game, walk towards it. There we go.
Nothing happens, obviously because
we forgot to do the most important thing,
which is the assigning of the canvas.
So we assign the canvas to each
one of those that run the game.
And now we should have fixed our problem Any day now.
Then we go down. There we go.
We press E to buy price 99 coin,
price 99 coin and priceline.com. Okay, great.
So now it turns off, it turns on.
And obviously the bi should also work.
We have an error which is something.
Present and the current Unity version.
Thank you Unity for this very bad update.
Any ways, let's continue on.
Now there is something to take into consideration.
We have the three types of items that we
can buy. That's good.
Okay, so we can for example,
say if this item is so on and so on, we do so,
so there is something better that I want to
teach you in this video and that is called the inner.
I'm not going to go into the slides because
it's pretty simple to understand.
We can create an enum.
It's going to be the item type.
And we can open up the brackets in
here and say that one of them is
the health restore, health, restore.
The second one is the health up grade,
and the last one is a weapon.
So what is the enum item type in on basically,
in its simplest terms,
allows us to create a new type of variable.
So just like we have the Boolean,
the canvas, and so on,
we also can create an enum,
which is the item type.
And now we can create a variable in here,
which is a serialized field of type,
item type, and it's the actual item type.
There we go. So as you can see,
the enum has capitalized first letter and on every word,
while the variable is not capitalized,
it's in the lower case.
So if we save this and go back in our game,
look how cool this will be.
If I choose the item type,
let me close down the box colliders.
You can see that I have
a drop-down menu and I can choose one of these items.
And this is something really nice.
And this is an upgrade from the previous section where
the enemies each had
their own box to take and so on and so forth.
And you have to untick something to take something else.
This right here is a much better way,
a much smoother way.
So this one will be the half upgrade and
the last one will be the weapon.
Great. So now we have
the item type we can choose to go from.
But there is one more thing we need to add in here,
and that is the item cost.
So serialized field and the item cost,
but this is not the thing that I was at.
I wanted to focus on I wanted to focus on
something called the switch statement.
But before I do that in here,
I'm going to create the update.
And I'm going to check if I am envy by zone.
Then I'm going to check if we have input dot,
get keydown, open this up, keycode,
key code dot e.
And in here what we can do is for example,
we can start maybe creating if,
let's say the item type dot something and whatever,
and start checking for
the items and buying based on that.
But we have, in this case,
something much more powerful.
And that is the switch case.
So let me show you what these switch case is.
So similar to the if statements,
it allows us for conditional code execution.
If we have a certain item type,
we want to call something different.
If we want to have the health upgraded,
we should call a different method
than the one where we buy a weapon.
So the switch statement is
going to be very effective in here.
And just like the if statement,
it works the same way we have conditional code execution,
but it's structured as a bit different and
more efficient when it comes to things like these.
So we start off by writing switch.
We add the case switch.
So in here this could be anything we want.
In our case, it will be the item type
that we have. In case one.
For example, let's say the item type is a health upgrade.
So in case 1,
one here says, for example, health upgrade.
We execute some code we break.
This is very important and then we go to the second case.
The second case let's say is the weapon.
Then we execute some different code,
we break, and then we check for the default.
Now obviously you can add up
how many cases you want to 123,
1520, 500 if you want.
But eventually we will have
to get some kind of default thing.
So what happens if we have no item type?
This is something that will not happen,
but just in case there is no item type,
we need to have a default break and
then We finished the switch statements.
So make sure you understand it.
We will see the implementation and just a bit,
but just look at the structure.
We then without Look what it is.
And let's go back into our code and implement this.
Before we create this switch statement,
we need to know if we actually have them money for this.
So first of all, we're going to check if
the game manager dot instance dot.
Let me see.
Do we have an access to the current going? No, we don't.
So we're going to create a public int.
And get current bit coins.
And this will just return current Bitcoins.
Save that, go back and hear instance
dot get current bitcoins.
And if we have more or at least equal to,
so if we have more or at least equal to the item cost,
then we can continue on.
And as soon as we buy the item,
then we are going to go to the game manager,
dot instance, dot, spend coins.
And the amount of coins we're going to
spend as the item cost.
But then what will happen after we spent our coin,
we should get an item.
So we're going to create the switch.
And in here we are going to say the item type.
So this is the case around which we are switching.
And now we are going to open this up.
So in case we have the item type
dot health restore and
it's not allowing me oh, no, it's not.
The small item type is
the big item type dot health restore.
We put the two points, hit Enter.
Here we are going to,
while in here we can either find objects of
type and it's going to be the player health handler.
And then we can go in here.
And what do we haven't
declared health handler damages the player.
Okay, So at HP to player,
at HP to player.
And how much do we want to add?
Well, we can choose in here whether 10 or 20
or whatever value you want to add, save that.
Brake had this and there we go, Save that.
So I paused there for a second because I wasn't
sure if we should use the file object
of type or if you want even better,
create a variable in here for the player.
So a private variable for
the layer and then set it right here and the collision,
this would be smarter and more cost
efficient because we know that
only the player can be
the collision in here because
of the layer that we've sat.
And find object of type is sometimes a bit cumbersome.
It's, it takes a lot of space from the processing, a lot,
a lot of processing power because
fine object of type just goes
through the entire list in
your scene and then chooses one of them.
So it depends on you.
Whatever you want to do really is up to you.
So where do we have the item right here?
So let's choose the first one.
That's the cost to 20.
Or know what? Let's just keep it at 10.
And this will restore a bit of Health.
And I'm going to go into the Prefabs.
And in here I'm going to add the spikes.
So or is it the pickups?
Let's add a bit of coin and here, 23 coins.
So we have money to spend.
And then we'll go into the Prefabs again and add a.
Where is it?
Where is it? Where is it?
Level object at the spikes right here. Run the game.
So I want to get hit by the spikes.
Okay? Pick up a couple of cones,
go down here and we're visit. So there we go.
We press the Iike.
Yep, there we go. We get tan, tan, tan, tan.
And if I tried to buy more, there you go.
We have 0, so I can not buy anything anymore.
There we go, so great.
That seems to be working.
In the next video we'll be using the health upgrade
will create a separate method and the health handler.
And as I've said, and by the way,
this error keeps popping up.
Don't worry about it. As I've said in here,
the fine object of type something that I don't like
particularly I don't like to overuse
defined object of pipe goes back to you.
I really hope that you try to
create your own variable, set it right here.
With that said, I hope you enjoy it.
I hope you are saving all,
singing all of your files,
and I'll see you in the next video.