Welcome back, My favorite game developers and this video,
when we run the game,
you'll see that we have the rifle.
When we switch a weapon, we have a pistol.
When we switch the weapon,
we have a shotgun.
So now we have a visual representation of which gone,
we are carrying with us.
So without further do,
let's not waste any more time and let's get started.
Oh, case.
So now instead of just changing the gun,
the pillars holding, we want to also
display this on the UI screen that we've created.
So let's go ahead and do that.
First of all, I'm going to make
the scene a little bit bigger so we can see properly,
I'm going to double-click somewhere here on the canvas,
whereas the canvas double-click on it.
And there we go. Now we have the huge canvas.
Let's go to the layers and let's
set that we can see the UI layer.
And in here this just for a moment,
turn off the fading image so we see what is going on.
And in here, what are we going to act?
Well, we want two things to represent
the weapons that we are using at the moment.
We want to have some kind
of image to represent the weapon and
we want some kind of text under that image.
So let's go ahead and get started.
The first thing I'm going to add is right-click in here.
And let's just first of all create
an empty game object just to
represent everything about gun.
So I'm going to say weapons, UI.
And under weapons you are,
I'm going to create a image first of all.
So image, this will be the Weapon Image.
And I'm also going to create
a text which is going to be the work.
What is it, the weapons text.
There we go. So now we have
a weapon image and a weapon text.
I'm going to click on the canvas here for a second.
Just open this up.
By opening up the canvas, I can,
wherever I click, I can still see the borders.
So I'm going to choose
the image and I'm going to drag it down to the side.
And you can see right here in the game view what
we are going to see all the time.
So I'm going to make this a bit bigger,
like something about that size.
And this will be the gun engineer.
And then we will have the texts to
write under it right here.
And let's just test this out and see how it works.
So in the image we will choose a source.
I'm going to lock this down just so we don't lose it.
And I'm going to go into the sprites guns.
And let's go ahead and add the shotgun. So there you go.
You can see now we can see the shotgun.
We can set it to the native size
to make it a bit smaller.
But you know what? I honestly like the way it was before.
Big and easy to see.
Now the weapons text,
we want to change a bit because we want to have this,
for example, the weapons name.
And later on we will learn how we
can change or create our own font.
But for now, let's just keep it as it is
just for the purposes of testing.
We can change the alignment
in here and we can set it in the middle.
We can make it bold.
We can do a lot of stuff with the text,
but one of them is changing the color and making it
completely white so we can see it in
the background, no matter what.
I'm going to make it a bit bigger
so we can increase the size.
There you go, you can see that's how we increase
the size and now we will increase the font size.
So there you go.
That is about how I wanted to make it.
So the weapons name right
under the actual weapon and we can even make
the box a bit bigger and increase the size of the words.
So there you go. Now we can
write the shotgun and so on and so forth.
Let's remove the wreck as target
in here because we recurse target,
as we've said, we can click on it.
We don't want to click on it.
It won't make any difference,
but let's just keep it at that.
We can change the horizontal overflow,
the vertical overflow, and we
can create something called the best-fit.
And this is really nice because now when we change,
you can see that the font will change with the,
or the, the size of the words will fit as to as big V.
And I'm not sure why I can say anything properly,
but anyways, there you go.
You understand what I mean by best fit?
You just saw what happens.
Okay, Let's continue on and stop rambling.
The next thing we want to do is we want to have
access or to the behavior of this UI.
So what we're going to do,
we want to first of all,
assigned to each weapon the appropriate sprite and name.
So I'm going to go into the weapons prefab.
So prefabs, weapons, select the pistol.
And this is where we are going to be setting everything.
So I'm going to open up the script right here.
And on the weapon system,
because each one has a weapon system,
we are going to add two variables.
The first one is going to be a sprite.
And it's going to be the gun image.
And the second one is going to be a string.
And it's going to be the gun.
Let's keep it.
A weapon and the weapon.
Weapon, neck. There we go.
So weapon Text, Weapon Image and what happened name.
And if I go back into
Unity after it compiles, There we go.
We can see that we have the image sprite and image name.
So we're going to choose the pistol.
Go to the pistol at the image.
Make this the stone.
And then we are going to go and to the rifle or
nano to go back to the Prefabs weapons rifle,
lock this down, find the image down here.
What's the name?
Let's just keep it at rifle without the assault part.
And what else do we have?
Less choose the shotgun,
know that the prefab shot gun, lock this down.
Finally, shotgun image, green here,
weapons name, shocked, gun. There we go.
So now each prefab has
its own garden image or
Weapon Image and its own weapon neck.
So next thing we need to do is we need two every single
time we change a weapon to fill this image.
Currently, if I run the game,
you'll notice that I have
the image right here, I can move around,
but if I tried to change my weapon,
the images don't change.
So now it's time to add behavior to this.
And we are going to do that in the UI Manager,
which is on the campus.
So let's open up the UI Manager
and let's see what are we going to do in here.
So first thing first is we need
a reference to both the image and the text.
So I'm going to create a serialized fields in here,
which is going to be of type image.
And we can use EMG because we have
Unity engine dot QI using up there.
We are going to call this the weapon image.
Let's just keep things simple,
just like they were before.
And in here we will have a text with which
is the weapon name.
Save that back in Unity.
And let's see now we can go ahead and lock this down.
And we can add the weapon image,
and we can add the weapons tax.
We can click on the UI canvas and make sure we
apply these changes all over our project.
Excellent.
Now, the next thing to do is to show you a challenge.
And your challenge is going to be to
create a method that changes the gut.
So first thing first,
you need to create a void method that
is public and takes two parameters.
An image for the gun and a string for
the gun name or
weapons name or whatever you wanna call it.
Then you'll need to set
the gun UI sprite using the image.
And you'll also need to set
the gun name text as the column name.
And finally, I will give you a small hint if you want,
if you don't and do the challenge.
If not, you can use the gun you I dot
sprite and the gun UI or gun name you are,
or whatever the name of the actual variable was.
Dot text, dot sprite to access the sprite and the text.
Remember this should be weapon and not UI image.
Oh, okay, so there you go.
Pause the video right now.
Go do the Chaldean.
Okay, welcome back.
So what are we going to do in here?
And we are going to create a public void and
change gone or change Open UI.
And we said that we are going to take in two parameters.
The first parameter is going to be a sprite,
and it's going to be the weapons image.
And know what, because we already
have the weapon image year.
Let's change this one to gun.
So there you go, gun image.
And we're going to also take in a string,
which is going to be the gun text.
So these are the two parameters that we are
going to create and now that we have them,
how are we going to change the images?
While if we go in here,
as I've told you in the hands,
if we click on the weapon image,
we can see that right here we have the source image.
So if I go, I'm going to say that we have
the Weapon Image Sprite
and it's equal to the gun image that was provided to us.
And then we have the weapon name dot text.
So we are accessing.
Let me go back in here and show you something.
So we have the weapons relaxed.
It is of type text but the component tax,
but we are accessing the text on the text.
So we have this text parameter or characteristic.
The components. So we are accessing that by saying
dot txt and it's equal to the gun text.
Save that.
Oh, great.
Now we can actually change the UI,
but the next step is to actually change
the UI of the weapons whenever we switch them.
And where are we going to do that?
Well, we are going to do that in
the switch weapon method 0 and 0.
There is something here I wanted
to tell you, I commented out.
Let me tell you right now,
if you've noticed when we play our game,
as soon as we start,
for example, we have the rifle, okay,
so we hit the tab key and it doesn't change,
not until we hit the tab key again.
So what is the problem?
The problem is that in
our layer right here we can see we have the current gone,
but we haven't assigned any value to it.
So it doesn't know what the value is.
So what I did and start and the player controller,
we can simply write these two lines of code.
And what these do is first of all,
we set the current gun,
which is an integer to the available weapon dot count,
and then we just switch to the gun.
So we start off at the rifle.
So now when I run the game,
or we can even something much better if you
want to start off with the first weapon that we have.
We can set this as the
available weapons dot count minus one.
So now, as soon as we start the game,
we will have the first gun with us.
Okay, so there we go.
Now we can switch immediately
and there's something wrong here.
Oh, okay, so again, it's that annoying error.
So back in games.
So as you can see now, as soon as we start,
we can hit the Tab.
May want to make this public,
but as I've told you in the previous video,
it's not that good of a way to get access to variables.
A much better way is to go in
here and create two methods.
The first one is going to be a public.
It will return a sprite is going to be
called the get gun image.
You get weapons image actually get the weapon image UI.
And I'm going to open up the brackets and
I'm going to simply return
the weapons and close it off with a semicolon.
And then I'm going to create
a similar method which is going to
return a string and it's going to be getting the weapon.
Get weapon, text, UI or name,
actually name UI or not.
We can't even Use the word UI.
But let's keep it just for the sake
of knowing why we are getting these.
So weapon name, save that.
So now we can access the weapons system.
And from that weapon system we
can access these two methods.
And with these two methods,
we can have the image and the name
of the guns without facing
the problem of changing
these variables by mistake or causing some sort of error.
So right now what we can do is
we can get the available gun.
Available weapon at the position current gun
or is it the current gun?
And we can get the component on this current gun,
which is obviously the weapons system.
And from this weapon system,
we can, what can we do?
We can first of all get what is it called?
Get Weapon Image.
And then we can
hit Enter here because the line is too long.
At a comma.
Copy this, and paste it in here at inaugural enter.
And what seems to be the problem here?
Why is the first one telling me cannot
convert method group 2, right?
Okay. Okay.
This is a method.
And get the name,
get weapon name UI.
There we go. We save that.
So now if you'd like,
I do recommend that we do
something here because we have redundant code.
We can set this as
a variable and then use the Get weapon and get an image.
That would be something smarter to do.
But for now, let's just keep it so we always know
what's happening after we change the weapon,
we set it to true,
then we access the UI Manager.
We get the available weapon at the current position,
we get the weapon systems script
and then we get the weapons image.
We send it to the change Open UI,
which is in the UI Manager.
And this should change
the image whenever we change the gun.
So back in our game,
Let's run and see what happens. There you go.
When we hit Tab, we get the shot gun,
we get the rifle,
we get the pistol which is smashed for
some reason because the pistol
is trying to fill the entire gap.
Let's see, where is it,
where is it? Where is it?
In the UI, the weapon image.
So obviously when we set it as the gun,
it's not that good,
but no worries about that.
As long as it works for you,
we can change this, make it smaller, make it bigger.
Maybe even a better idea,
maybe changed the way this image is represented.
Bother me too much.
If it bothers you,
and I do recommend maybe we'll
see how we can fix this later on.
But for now we can see that it is working.
The only thing that is not working.
As, as soon as we run.
We can see that unless we switch the gun,
we have the first one which is the weapons name,
just like that, a blank image and a blank text.
So what we can do is in UI Manager in here.
So I'm going to select this.
I'm going to right-click quick action and refactoring.
And I'm going to call this the setting weapons.
Weapons. You, I hit Enter.
And now I can go and start.
I'm not going to switch the gun because I just noticed
something that this does not work unless we have the tab.
So all that we're doing in here
is just setting the current gone to,
to have it work immediately.
You can set it to 0 if you want.
But let's remove this and instead
we are going to be setting weapons UI.
So setting weapons you.
As soon as we start.
And let me just select those and hit
Shift tab a couple of times
just to make them looking a bit better.
We save that as I've told you,
the smart thing to do is to change this
and put it in one of the variables.
I recommend you can go do that.
And with all of that set,
Let's go back into our game and let's test this out,
see if it works from the game.
And nope, that didn't help at first.
Why is that?
So after doing a bit of research,
I realized something and
understood where the problem is coming from.
So in here we are trying to set the weapons UI,
which is here, and we're trying to
access the UI Manager Don instance.
The problem is, and I've noticed that right here,
if I run the game,
you'll notice that I have this arrow right here.
And you can see that it object
reference not set as an instance of an object.
So I double-clicked and I looked in here
and I noticed that for some reason
the player script is being instantiated or a
starting before the UI script is starting.
So we still don't have a reference to the UI Manager.
Now, what we can do is we can try
to make this and awake instead
of of a start and
await starts just a little bit earlier than the start.
Let's test this out and see if it works.
If it does, then we saved ourselves a lot of problem.
I'm not sure this would work,
but let's test it out.
So in-game, run the game.
Okay, so there we go.
It did work. How cool is that?
Who we saved so much time,
I wasn't sure it will work.
Now another thing is the pistol being smushed.
We can go into the image right here and make
sure to click on the preserve aspect.
You can see the pistol will stay the same.
So I'll stop the game and Presets,
save that and make sure to apply the changes.
So now when we run, there you go.
You can see that the PESTEL is set correctly,
but what's the problem?
The problem is that I have
a rifle and my hand yet the pistol is being depicted.
Why is that?
Well, because again, if we go back, we'll save that.
And for future purposes,
always make sure to set
the instance and the awake not in the start.
So now this was an amazing opportunity for you to
understand the difference between
the start and the awake.
Awake start just a little bit before we start.
Let's continue one, why do
we have the pistol in the beginning?
It's all because of the current gone available.
So the current gun is being set to dot count minus one,
which is the pistol.
Because the pistol, as you can see in the dooms player,
is the count minus one,
which means element two.
So how are we going to fix this?
Well, what we can do is two things.
We will simply set the current gun to be.
I think maybe one.
That way we will start off on the right position.
So back in here,
we will run the game.
We are holding a rifle and we have a rifle represented.
And so later on,
I'm sure we will find a smarter solution to this,
but for now, we'll just make what works.
I hope you enjoyed the video.
I hope you are saying
all your files and I'll see you in the next one.