[MUSIC]
Hello again, this video is gonna be somewhat a problem solution style lecture.
And the problem, let me articulate it for you, the problem is that when
we finish our game, or lose our life, or want to restart, we have a score, 83.
When I click Play Again, and go back to my first screen and
click start, my score is still 83, it hasn't reset and it goes up from there.
So if I was to lose the game here,
of course the ball's gonna go way up to the top and [SOUND] lose the game 1826.
Play Again 1826 is still hanging out, okay so
that is our problem and that is what we will be solving in this lecture.
I wanna reinforce something for you and then ask you to do a challenge.
The aspect of our approach I wanna reinforce,
is this concept of in Script A we have a public method that we have created.
Which controls an aspect about code, it does a particular thing, it updates
a state or changes in variable or tells the game to take a particular action.
And then we have in Script B, we call the Public Method that's in Script A,
cuz we're requesting that something happens, it's not happening in Script B.
But Script B has an awareness that says, now I would like you to do a particular
thing, you own that particular thing, Script A, so could you please do it?
And this is a very safe way for us to structure our code so that Script B isn't
accidentally going and doing something that Script A isn't aware of.
If a particular thing changes, for example, our score,
we go and we look at where the score is being managed or
controlled, and we know that's what we need to be looking at.
Okay, so here's the challenge for you, I want you to reset or restart the score.
So here's our paradigm again, we're looking in GameStatus.cs for
our public method.
And in here, we're wanting the game persistence, so the don't destroy on load,
or this whole GameStatus.cs, we want it to destroy itself.
That's what we want to do in here, we want it to destroy itself.
And what we're requesting is that SceneLoader.cs is calling a public method,
calling this public method we've created in here when we want the game to reset.
And just a quick note on that, let me open up my SceneLoader.
Okay, in here, there's a place where we say we want to load this dot scene.
That's the place that I think that it's a right time for us to say,
let's reset our game at that point.
So here is your challenge, create the public method and call the public method,
so that our game is reset.
Take that on and I'll see you back here when you're done.
Okay, I really hope that you stopped and thought about that cuz we've done this now
three or four or maybe more times, this calling a public method.
So hopefully it's really starting to sink in, if it hasn't then stop the video and
really look at it and really take this challenge on.
Now, what have I got here?
SceneLoader, Block and Level, I don't actually have open the script that I
made which is game status Let's open that up.
GameStatus, great, I'm gonna drag GameStatus and
SceneLoader they're the two scripts that I need.
Within GameStatus, we need to find a place where we are going to say to create a new
public method I'll do this down the bottom, underneath AddToScore.
We'll say, public void and
I'm gonna call mine reset game.
And we're not parsing in anything, open and close curly brackets,
what are we doing in here?
Well we want to, up at the top, we say Don'tDestroyOnLoad as we're doing our
singleton but at some point we have to say well, now it's time to destroy yourself.
And we're gonna do that whenever something else says now it's time
to reset the game, can you please Destroy(gameObject).
gameObject with a small g, cuz we're talking about this particular instance of
gameObject, we're not talking about the class GameObject,
that would be a capital G, it's an instance so it's a little j.
Okay, this is why we're saying, destroy yourselves, now let's go and
call that from a particular place.
I'm gonna go and SceneLoader,
find this part where it says now it's time to load the StartScene.
So the SceneManager loads Scene zero, and by the way,
while you're there, we need to call that public method.
So firstly we need to say, FindObjectOfType, it's only object,
we don't need objects in this instance, FindObjectOfType.
And what are we looking for?
Well, we're looking for GameStatus,
GameStatus()., and it will be ResetGame(),
cuz we're not passing anything in and semicolon.
Okay, so this us calling the reset, let's jump back in and see out behavior now,
click on Play.
I should get 83 when I destroy this particular block and
then in our game over screen 83 is still there.
I quite like that, I think that's a good time to do it.
We'll click Play Again 83 is gone don't destroy unload that was there a second ago
is gone and when I click Start, we get a new don't destroy unload that
is protecting our game status and our score is back to 0.
If I get a score 249 and then Play Again and its back to 0 again.
Excellent, okay so mission accomplished on that,
I wanna do one last thing in the selection while we're here.
I know a couple of you have been asking about how do I rename a class if
we've renamed it incorrectly.
And I've gone and called this GameStatus, at this point I'm thinking, you know what?
Let's call this GameSession,
because that gives a better indication of the fact that this session exists.
And then when we finish, when it's time to restart,
then we want to destroy that game session and start a new game session.
If you just go and rename it here and there, and
rename it in your class sort of in your script it might not work.
So I'll show you the way that we go about renaming.
If we double click on the GameStatus and Ctrl + R + R.
So a Ctrl + R and then hit R again while holding down Ctrl,
that will allow us to do a Rename, or if we right click and go to Rename in menu.
So if you're on Mac and the Ctrl + R + R doesn't work,
then you can highlight and click on Rename.
And it says here, make sure that it renames in multiple files,
you can see, Rename will update 4 references in 3 files.
If there's any particular option you see that says,
only rename in this file, then we don't want that at this point.
We wanna rename it everywhere, including references in other scripts where we
are referring to it, for example, in our SceneLoader.
It acknowledges aha that is also in there.
Okay, so we will rename this, highlight once it's highlight
in green it's gonna rename it GameSession, hit Enter, okay.
But my script is still called GameStatus and
Unity is not going to like that at all.
So we come back over into Unity, I find GameStatus, I click on it,
and then wait a little bit, and then click on the name once again.
And that will allow us to type straight in there, we could also right-click and
click on Rename, that will allow us to rename.
I'm gonna change this to GameSession, whoop, spell it correctly, Session.
Right, that's updated in there.
And then the final place, this is not critical but
our Game Object is to called Game Status.
We know that this is a prefab, so I'll find the prefab and
rename it prefab itself.
Little hint here, if you If you right click on a Game Object that is Prefab,
that's an instance of a Prefab.
Right click and then Select Prefab,
it will show us in our assets where that Prefab exists.
Again, right click Rename and instead of Game Status, we'll call this GameSession.
Hit Enter, and then you can see over in our hierarchy its updated game session and
that's in level two.
It will also be updated in Level One as well because it's a prefab and
they are linked.
So now, everything is hooked up, we've renamed our prefab
which also in the process we renamed our script and renamed our class.
So that's how we do our renaming, great stuff well done,
I'll see you in the next lecture.