Welcome back My favorite game developers
to the last video and this one
we are going to fix all of
these small details related to the UI display,
related to the Bitcoin coming from level to
level and all of the other fun stuff.
So let's not waste any more time and let's get started.
Oh, okay, so now we need to fix
the problem of our enemies not finding the player.
We also want to make sure that
we have the game manager as
a singleton pattern because we obviously want to
keep our bitcoins going from level to level.
So for that, I'm going to issue a challenge,
and your challenge is to find
the player and find them on.
So first of all,
make sure that the enemies keep
looking for the player until we find them.
So this will be need to be a while loop
inside of the update and
the enemy controller. That's a small him.
We want to keep our Bitcoins from scene to scene.
So make the game manager
a singleton pattern just like we did with the layer.
Make sure you do the same for the game manager.
Does it appear in the hierarchy and the resident destroy?
So remember when we made the app layer Aidan destroy,
it appeared in the hierarchy under a certain tab,
which is did onDestroy.
Will the game manager also appear?
So test it out because we need to make sure that
the game manager isn't a child of the managers.
So the only way to make sure that he
does not get destroyed is to make
sure he is not a child of
the whole managers game objects so well that information.
Pause the video right now and go do it alone.
Oh, okay, welcome back.
So first thing first
we're going to go into the enemy controller.
Let's scroll up to update and an update in here.
I'm going to check that while
the player to chase is equal to null,
we are going to go ahead and get the player to chase
or set dipolar to chase to be find objects of type.
Object five, sorry.
And it's going to be the player controller.
And it's going to be
v dot transform because declared to chase,
as we remember, is a transport.
So we access the transform after refined
the object which has the player controller on him.
So this should work,
but as long as we're here,
let's go to the where is the game manager?
Scroll up the instance.
And in here I'm going to find the player controller.
And it plays now, okay,
So player controller, it's right here.
I'm going to copy
exact same code and
paste it in here and remove this instance.
Save that. So now we have a singleton pattern.
We check if there is an instance, we destroy it.
If the instance is different than null,
we destroyed and we set a new instance.
We save all of that.
Let's go back into our game
and let's see what we need to do in here.
So the first thing we need to do is we
need to open up the game manager in here,
and we need to remove the game manager out of here.
No, it will not allow me that we see in the
prefab if I already have the gain managers.
So Manager, the game manager.
So I'm going to open the managers
and I'm going to remove the game manager.
So this will remove the game manager
from all other scenes.
And I'm going to make sure that I
just have the game manager in here.
So no other managers,
one, sorry for that.
We need the game manager only in the scene.
So now we have the game manager,
we have 0 but coins.
Let's save all of that.
Make this just a little bit bigger.
Let's run it again.
And let's see what happens.
So we pick up the weapon,
becomes a bit coins,
we get hit once.
Let's go to the next level.
Let's see, we go to the next level.
And there we go, We are on the new level and we can
see that for some reason,
do we have the enemies following?
Nope, they are not following.
There is something wrong.
And as you can see, obviously the bitcoins
and the health have not been updated.
And that's because if we look on the player,
if we look on the don't destroy the game manager,
the Bitcoins also haven't been.
So if we look on the player,
you'll notice that his health is 490,
but it's not being sent right here.
Why? Because we only update,
let me see, and the player health manager,
we only update the player health once
we get hit and not when we actually start.
So we'll need to fix that also.
But before we do that,
there is something that is
not quite right with the MS towards.
So I'm going to pause the game right
here and let's see what the problem is.
So you can see that each one of
our enemies is not being able to find us.
So I'm going to double-click.
And the problem is with the player Chase right here.
So let's see what we are going to do about that.
Oh, okay, so I went through
a couple of things and finally found the solution.
So the problem in here was,
and it's very simple,
we just need to move
this while loop up here over all of this.
And we might want to
extract this into some kind of method.
So I'm going to call this method
finding player or making sure we have the player.
So why do we move it up here?
Well, because if we try to call
the moving towards, save that.
So what's the whole problem in the whole problem and
the whole thing that is causing this is when we
go to a new level before the layer,
which is the one that doesn't get destroyed,
gets to the next level.
The enemies already have a target,
which was the previous player.
So once we destroy him,
we can no longer find him.
So if we try that,
say we wanted them to the new level,
we have all the players chase set
right here in the start and then we destroy him.
And then we try to chase
the player or move towards the player,
and that's where the error was coming from.
So right here we are trying to chase the player.
And once there is an error in here,
nothing under this works.
The update stops, everything stops.
So that's why we need to be having the finding player.
First thing we do an update before we continue on,
while we might just need
the if statement in here, I believe,
but let's keep it as well,
just in case it takes a bit of five.
So with that out of the way,
if now we run the game,
we should see that when we go to the next level.
There we go, we have no errors.
We open this up and there we go.
The cowardly enemies are
running away and they are shooting at.
So what that out of the way,
next thing we need to do is obviously we
need to update the player UI.
So I'm going to go into the UI Manager.
We stop the game. Yep, we did.
Back in the UI Manager.
Here I'm going to create a method that is going to be
the public void update layer UI.
And we are going to do a couple of things in here.
So the first thing we're going to do is we're
going to create a player health handler.
Deep layer health colored,
and it's going to be equal to null.
And you'll see why in just a bit,
because we are going to create
a while loop that will check for the player health.
And while it's null,
we are going to keep looking for
the player health to be equal to find objects of type.
And it's going to be the layer health handler.
And once we find that,
we can exit the while loop.
And then in here we are going
to go ahead and first of all
get the health slider dot max value.
And it's going to be equal to the player
health dot get max value.
So obviously we need to create some kind of way to
access the max out on the player health handler.
What do we have any year, Okay,
so they are serialized field.
Let's go ahead and create a public and
get layer max health.
And we're just going to return V-max.
Have not.
We already have one of those.
No, we don't.
Let's go ahead and create the good layer,
current health also, as long as we're here.
So the current health and what else do we need from here?
We might need the,
Nope, I think that's all that we need.
So now we can get max health.
And then we'll need to set
the health slider dot value to be equal to
the layer health dot get current health. Save that.
What else do we need to do?
We also need to find a way to update our text.
So I'm going to say the, what was it called?
The Health text, was it called the health tech step?
There we go, and it's public also.
Great.
So scroll down here.
So the health, health flexed dot
text will be equal to the player health dot get current.
The current health.
And it's plus the slash.
And was the max health.
So Player health dot GetMax self.
There we go. So everything is set,
but I'm going to do something in here.
And that's right, a debug dot log.
And I just want to keep track of the same acts Health.
And let me just get this one.
And this will make sense in just a bit.
So upset this one in here.
And you'll see why in just a bit.
So I'm going to save all of that.
I'm going to go back into Unity.
I'm going to run the game.
I'm going to pick apartment becomes the Bitcoins.
Get hit.
Please get it. Okay, So there we go.
Go through the world,
go through the portal, go to the next level,
and you can see that it's 100 in here,
even though if I press on the layer, it's for 90.
So what do you think is the problem?
I'm just going to give you a couple
of seconds to think about it.
The problem was and I'll tell you what the problem is.
First of all, we need to update player health.
So update the layer health or player UI.
Now let's go ahead and test it out.
Maybe you saw it in the debugger.
I just noticed that we didn't even call them methods.
So there you go. Debug works,
I'm going to get it.
Go through the larval,
go through the portal.
And what's the health?
So the max health is 50 and the current health is 0.
What do you think is the problem?
Well, the problem is
just like we said in the previous video,
we already had a player in
the scene and that player was destroyed.
So once we want to set this house bar and the tax,
they are being set based on the old player.
So in order to set the new player,
what do you think we should do?
While there are many solutions to this problem,
but I found that the best one is to just have
a simple way of converting this
from a public voice to a public art and numerator.
And then we are going to yield return.
New. Wait four seconds.
And let's just wait 0.1 and
it will be the time while the,
don't forget, we have a fainting image
that we've turned off just
for the sake of speeding the process of testing out.
So now what will happen is we will wait 0.1
seconds and during that time
the older player will be destroyed,
the new player will be found,
and we will immediately get our update.
So in here, instead of updates player,
I'm going to start a co-routine,
which is going to be the update layer health you.
I save that back into our game.
We run the game. We get it.
We go out, we go through this one, and there we go.
We can see that our health has been
updated and it's very seamless.
So there we go,
We can see everything is working perfectly fine.
And first of all, we also have the Bitcoins,
so we need to fix that also in here.
And what we also want to do is we want to make
sure that we spawned in the proper position.
So currently, respond right next to the portal,
which is not helpful.
Our goal is to get to
that portal and not just spawn in there.
So I'm going to,
first of all in here,
Let's see what we need to do.
So we need to do we have the Bitcoin text,
the BTC effects. There we go.
So BTC text, the text is equal to find or no,
we have the game manager, the on instance.
Get, but continous. We go,
I believe this one returns
the bitcoins all know this as Bitcoin.
So if I go to the game manager to have,
get current Bitcoin, so I
can get current Bitcoin, save that.
And this should be working except for it's not
because you cannot implicitly
convert the type and two strings.
So the two string,
this converts anything play string, save that.
Let's test this out a little bit from the game.
And now we pick up a coin and we go to the next level.
Okay, so now we should see we
have in here ten, there we go.
It takes time, but it will not be noticeable,
especially once we have the fate image,
but now we need to spawn the player at a certain point.
So I'm going to go into
the level manager and I'm going to
do this at the level manager.
And I'm going to scroll down in here.
And I'm going to create a new method.
So under here we are going to create
a new method which is a public void and
its spot layer and point and input player in point,
we are going to get the player controller dot instance,
dot not instantiate, dot dance,
play a bullet controller.
Okay, So layer controller instance.
And we're going to use
the dot transform position
and it's going to be equal to some kind
of spawn point, the position.
And now I'm going to copy this and
create a serialized field up here.
Of the transform and it's going to be the spawn point.
Scroll down and see how that looks. Yep, there we go.
So what happens in here is we put the player on
the point from the start.
Where does it start?
In here we are going to put layer point.
We save all of that.
And what happens in here is as
soon as the level manager starts,
remember it's not a ton destroyer,
it's not a single combat on the player controller
dot instance dot position is
put immediately on the spot point.
So back in here,
we already have these palm points.
We are spawn points 1.
So I'm going to create a new in here and it's going
to be layer on spawn.
And where is it and the scene.
Let's see, and it's going to be about here.
Save that if we go to another level,
we can actually go ahead and override this.
So in the manager override
and you can choose what do you want to override?
Keep in mind, we don't want to
overwrite the spawn points because
for every single level
there will be a different spawn point.
So I'm going to apply override
the prefab manager's level manager,
maybe also apply overwrite prefer manager.
There we go.
And here the player spawn points.
Let's go ahead and
also apply the prefab to the whole prefer.
Okay, great. So make sure you don't apply.
All, save all of that.
And now I'm going to run the game.
I'm going to go into a new level.
And we should. Nope, we didn't.
That's because because we oh, okay.
Okay. Okay.
So the problem is here and 11 manager,
what's the problem in here?
If we scroll up to them,
we spawn point and the level manager.
What seems to be the problem?
Also, there we go, It's in here.
We need to set it. And now we need to go to
the level managers and apply the changes to.
So now when we run,
we should see if we go to the next level.
No, it's still not working.
So what's the problem?
The variable has not been assigned.
So I think there is something not rapidly set right here.
So apply to prefab level manager.
And what else is the problem?
So I think in the other level,
the spawn point isn't getting properly set.
So I'm trying to apply the changes.
Maybe this time it will work.
Nope, it still doesn't because if we
go to the manager's level manager,
player spawn point on the level manager,
it is not even set.
So what do you think of a problem in here is,
I think we made some kind of mistake
because everything in here has been reset.
So you can see right here that these are not being set.
So I'm going to apply all for now.
This is not the best idea,
but let's not worry about that for now.
I'm going to go to the next level and make sure that
we finally spawn on the proper point.
But right now we will have also spawning.
And here, as you can see,
but we don't want that because
each level will have its own enemies,
will have its own levels, sorry,
waves, and so on and so forth.
But as you can see, everything works.
The only problem still in our game is
the fact that when we go to the door,
this pops up that first time and when we open it,
when we go away,
it does not go.
I mean, the words do not appear
again and the door does not close again.
Why? Because in the room
that you've created and this was a mistake by me.
We turn off the door.
So we turn off the door,
but the text display is always on the door.
So I'm going to leave this as a final challenge to you.
Do outside of the realm of the scores.
All that you need to do is
remove this text displayed from
the door and put it on the portal right here,
and then move it right here.
So the Canvas will need to be knocked on the door,
it will have to be on the portal and you'll need to
turn off the door on and off based on the text,
there will be a bit of changes to the code.
But I think at this point,
you have become such a good game developer at a,
such a high level with all the long,
long videos that I've taught you through.
I hope you enjoyed this video.
I hope you enjoyed this whole course.
I hope you learned a lot.
I hope you enjoy the journey and the process.
And I hope you weren't too angry when I made mistakes.
I hope you are liking
the transparency that I'm showing you.
And if you haven't already,
of course, leave me a review,
make sure you join the discord
and I'll see you
maybe not in the next video because this one,
this is the last one.
Maybe someday in the future,
we will keep in touch.
No worries about that. Thank you so much for watching.
Goodbye.