Welcome back, my fellow game developers.
In this video, when I run the game,
you'll see now that I can use a rifle,
I don't need to go to
the hierarchy and change the weapon.
I can press on the tab and you
can see that I have the pistol right now.
I press the Tab again.
I get the shotgun.
I press the Tab again.
I have the rifle.
I press the Tab again,
I have the pistol and so on and so forth.
I can do this all day.
Shoot them with a shotgun,
should the mother rifle should then with a pistol,
I can do whatever I want and nobody can stop me.
So let's not waste any more time and
let's get to switching weapons.
Oh, case.
So time to switch up our weapons.
The first thing that we are going to
do is that we are going to get
a reference to all the guns
that are available on the player.
Now, this is a point of debate.
Where do we want to handle all of this?
Where do we want to have the list of
information about all the guns and so on.
And I felt about it.
We can add a script to the weapons
on and make sure that everything goes smoothly here.
But actually, I think maybe the player should be
the one who is handling the available guns that he has.
So I'm going to go up and
player and I'm going to remove all you know what,
Let's keep it as a reminder of how
sometimes we are not very attentive and we make mistakes.
So backup here.
I'm going to go ahead and create first of all,
the reference to all the guns that we have.
So it's going to be a serialized field,
which is going to be a list of type,
the weapons, weapons system.
And like that.
And me scroll down a bit and
I'm going to call it the available
guns or available weapons.
Let's just call it available guns.
And it's equal to a new list of weapons system.
Save that.
Now the next thing we want to have is a
variable which is an integer that will
keep track of the current gun that we have.
So I'm going to create
a private int and it's going to be the current gun.
Save that.
So what are we doing in here?
What we want to do is we will want to get
a reference to all the three guns
that were holding or whatever,
how many guns we have available guns.
I'm going to lock this down, select.
And before I do that,
let's make sure to override the dune player apply all.
So we know we have the guns on him.
I'm going to lock them down and
I'm going to select the rifle,
that pistol, and the shotgun,
and going to add them to the available guns.
So as you can see now we have
a reference to the shotgun rifle and the pistol.
So what I'm going to do is I'm going to
create some sort of a loop that activates.
And this loop will go through all the weapons and
turn only the one that I want.
So the current postal and every single time I'm going
to increase the current pustule one-by-one.
So I know this explanation is a bit confusing.
Let's go ahead and implement it
and it will make a lot more sense.
So in here I'm going to first of all create
the method which is going to
be or know what before I do that,
let me just remove
the player shooting and what does this all do?
This, get it down space, okay,
so dashing player, so right-click,
quick action and refactoring extract method.
There we go.
So dashing, player or player dashing.
I think it makes more sense. Control RR.
There we go.
So as you can see,
I know all the shortcuts
to solving mistake because I've made so many.
Anyways, we are going to go ahead and switch the gun.
I'm going to immediately create a method
in here so we don't go and extract.
So it's going to be a public void switch gun.
And in here, first of all,
we will want to check which kind
of key we want to press in order to switches the gun.
So it's going to be an input that get C0,
get keydown was a key code.
And you can use whatever you want.
And let's use the tab.
Nope, not the escape.
There we go. So when we press tab,
we want to switch
our weapons and let's call this right here.
So switch gun, switch guns with sharpen.
I'm a bit confused.
You can call it whatever you want.
So gun weapon is the same.
We want to have anything other than guns as weapons.
Anyways, let's continue on.
The next thing we want to have.
We want to make sure that we actually
have guns in our possession.
So we are going to check if
the available Ghana's dot count is greater than 0.
And you might be thinking, well.
Course we are going to have a gun.
Michael, What are you talking about?
Well, later on when we start buying guns,
when we start picking up guns,
you'll notice that maybe at
some point the player won't have guns.
So what this will avoid us a huge error.
And I'm just going to add a warning in here.
The debug dot warning,
and no guns available.
Pick some up.
So this is different than the regular log.
This is a log warning and we also have a log error,
and this will be in a different color.
And the console, as you can see,
these are errors and
for some reason we have the clear preview cash.
This has been going on for
a long while and I've looked it up.
I've tried a lot of things,
updating or everything. Nothing works.
Apparently it's some kind of problem in Unity,
but it doesn't affect our game,
so don't worry about that.
I'm going to clear it with this button.
And you can see those are errors.
We have logged, regular logs and we have warning logs.
But let's stay focused.
What happens when we know that we have a gun?
We are going to first of all get
the current gum and increase it by one.
So this makes a plus, plus.
After we do that,
we're going to check for
something that is very important.
And that is after we go through all the guns.
So if we are increasing and
the current gun which has now been increased by 1,
is greater than the available guns dot count.
We want to go back to the first gun.
And how do we do that?
We reset the current gone back to 0.
So what's happening in here?
Let's say we have the three guns.
That means the available guns count is three.
That means that we start off at 012.
The two is the last index of
the last element is the index of the last element.
So what happens in here,
once we press the tab,
we check we have
available guns increased by one electron.
We will be turning on that gun.
But for now, we make
sure that if we have the current gun,
so let's say you are at index two
and the available gone count is three.
So we become, the current gun becomes three,
which means that we want to reset it back to 0.
So we then grab the first weapon
and step and avoid a huge error.
So what that done,
what else do we need to do?
Well, there is something
that we need to make sure of and that is,
if I go back in here.
So remember how we were using the different gums.
We don't just turn on the rifle.
So if I click on rifle,
we don't just turn on the rifle and start shooting.
We first turn of the pistol and
then we have the rifle on and we can shoot with it.
So this is going to be your challenge.
Your challenge is going to be to
set all the weapons to enact.
So create a for
each loop to go through all the available guns,
set all of them to be inactive.
So all of the all of them to be inactive.
And then set only the current gun to be active.
And I'm going to give you a small hint if you
already know what to
do or you feel you want to do it on yourself.
Pause the video, go do the challenge.
If not, this is what you will need.
You will need to access the available guns
on the current gun.
And you'll need to access the game object
because the available guns are of pipe organ system,
so we can just set active to that.
We need to access the game object
that holds the current gun,
then set the active to true or false.
So pause the video right now and go
set all the weapons to an active.
Okay, welcome back.
How did you get on with that?
Are really, really, this was an interesting challenge,
using a for each turning things
off and really understanding the mechanics.
So first thing first,
we are going to create a for-each loop.
And in here this is going to be the weapons system.
We will call it the weapon in available guns.
Seems kind of weird,
doesn't it? Know what?
Let's rename this from
available guns to available weapons.
There we go.
And we'll need to make sure to set them back in Unity.
Because when we change the name of a variable,
you'll see that now the list is,
nope, it's still good.
Why?
Because we didn't save.
When we save, now we should
see that everything disappears.
We need to set it back again.
There we go. You can see it's at 0.
This is something to keep always in mind.
So do that before we forget to avoid any mistakes,
save that, okay, Now, back in Unity,
what do we want to do in here while we
are looping through all the weapons.
So what I'm going to do is I'm going to go
ahead and use the weapon.
Dot.
Gameobject dot set active to false.
So what happens in here?
We have all the available weapons.
We set all of them to false.
And then what we do as recheck
the available weapons at the current gun,
the gameObject dot set active.
True.
So why are we turning off
all the guns and not just the gun that we are using.
This was something I was also thinking about.
But also later on when we start switching amine,
when we start picking up weapons,
will want to switch the gun.
And it's, it's much safer to turn
everything off and then just
turn the thing that we really need.
Maybe sometimes the ordering
of the weapons will be different.
And just turning of the one that we have right now,
maybe we'll turn on
something different that we don't want.
So I took the extra precaution
of just looping through all the weapons,
setting all of them to false.
And then just, I think
the one thing that we need to true,
I hope that's understandable.
And if you want to create
some kind of more complex mechanic,
I really, really would be happy to see it.
You can go and discord and show off your things there.
So I'm going to go back
into my game right here, save that.
And I'm going to go ahead and let's see,
run the game and see what happens.
So I have the rifle.
You can see that it's turned
on right here in the hierarchy.
If I hit the tab,
if I hit the tab, there we go.
I have the pistol. If I hit the Tab again,
I have the shotgun.
How cool is that?
You can see everything in the hierarchy.
Let's try this again.
Rifle, rifle shots, Pistol,
Pistol shots, shotgun, shotgun shots,
and so on and so forth.
So I hope you enjoy it,
makes sure to commit your changes.
And I just mentioned this court,
I hope you already knew that we
had a discord going there.
Show off your things that we see.
Um, okay, so I haven't opened
this course just to stay concentrated.
I always reply there as fast as I can.
Sometimes I don't reply as fast as should be.
It's because of course, MyTime,
we all have 24 hours and because everyone
is on a different time scale or whatever,
would have been different places in the world.
We have an amazing community.
A lot of people are there because of
the way we handled the mindset that we have,
that we are all students.
I always learn new things when I'm
helping someone and that's
what you should always be doing.
Don't be afraid to make mistakes.
Try to help as much as possible.
Joined the discord.
Leave me a review.
I hope you're enjoying and I'll see you in
the next video where we will actually
be not just seeing
the weapons right here in the players are,
and we will also be able to see them in
the UI canvas that we've created.
So with that said,
I'll see you in the next video.