In this lecture, we are creating some scene persistence.
So when I kill some enemies and grab some coins, when I die, those things are remembered and gone.
But when I die, another couple of times they come back because we've restarted our game.
So let's get started.
Let's start the process in our hierarchy by right clicking Create Empty.
I'm going to call this scene per persist and we'll reset that on the transform right click Reset.
Now I need to create a script in my scripts folder.
Right click Create C-sharp script will also call this scene per assist.
Now, similar to the logic of when we were having our game session and we said, because the game session
has a singleton on it, anything underneath that is also going to have that singleton logic applied.
So in our case, the canvas with the lives and the score is going to persist.
I'm going to do the same thing with scene persist.
However, underneath that I'm going to be placing my coins and my enemies and anything else that I want
to remember that if I collect one coin when the player dies, that coin is still collected until we
restart that level or restart the game and need everything to kind of reset back to where it was.
So click back on our scene, persist game object.
I'll drag my scene persist script over in there.
Now that I've got those two key elements, I can go to prefabs and prefab my scene persist so that as
we change our code everything is cool.
It'll update now prefab and I can click on same persist and drag underneath here my coins although I
do want to group them first so right click create empty and we'll call this pick ups and then underneath
pick ups we'll grab our coins, drop them underneath pick ups.
That's great.
And then pick ups we'll put underneath scene persist.
We don't want to go and update our or override our prefab because we don't want these things.
These this is content that's specific for our level.
While we're here for good measure, I'm going to right click create empty and create some enemies once
again reset the transform on that and we'll drag in a goober or two underneath my enemies.
There we go.
So click on that.
Where are you hiding, Mr. Goober?
You're way over there, Prop.
Drop you into the scene and we'll grab the other goober drop you into the scene as well, just so that
we can be testing these things out and we know that it's actually working.
So we've got these things enemies and we want to drag enemies underneath scene persist.
So those things should all now persist as we implement our logic in the script.
So let's start that process.
We'll jump over into our scripts, fire up, scene, persist and we go nice and fresh.
I'm going to well, I'm here top left click on Explorer open up my game session double click on that
closed scene Explorer again.
So we've got plenty of space.
I'm going to highlight my awake code block in here that's got our singleton on it and copy that so we
can reuse our code a little bit.
And you don't have to sit there watching me type everything and I highlight my start and update and
remove that.
We might put it back if we need it, but don't for now.
Paste in the week we'll go through and just change some of the details in here.
So some game sessions we will double click F2 that will allow us to rename.
So instead of num game sessions can be number of seen persists.
That's what this is called.
Same persists instead of finding of type game session we're finding of type seen persist.
Okay, so that's a singleton basically saying if you've got one already, you don't need another one.
That's going to be great when the player dies and then respawns and we restart the level, there'll
be a seen persist there already.
However, it's not going to be so good if the player exits the level.
So what happens if you exit the level?
We've already got a scene persist that has all its information about this is what I believe should be
contained within my scene persist and it's going to destroy any scene persist that we create for level
two and level three and level four and so on.
So what we need to do is have some logic in here, very simple.
I'm going to keep this whole script, this whole class, very simple.
So it's easy to understand we need one more thing in here.
But before we do that one thing, let's go and create ourselves a little bit of content.
So I'm going to save up level one, jump over into level two and we'll pop in here.
Our prefab scene persist.
Where are you seen?
Persist There you go.
Prefab grab our enemies and drag them underneath scene persist grab our pick ups and drag them underneath
scene persist as well.
So these should now be seen persisting if we've done this correctly and I save that and just go to level
three while I'm here so we can do proper play testing on this.
All my goobers just mushed around wherever he is, I'm going to right click create empty and tidy this
up by creating enemies right click to reset our enemies game objects.
I will grab all of the goobers except not the player.
You're not a goober.
You stay out of here, drag them under enemies.
That's good.
And then enemies we can put underneath game session.
Do we have any coins in here?
I just quickly chuck a couple of coins in here and our players should probably go back to where the
player needs to start off, which was where?
Up the top here.
Down the bottom.
Down the bottom here before the bouncy bounce just there.
And we'll put in a couple of coins at the start so that we can make sure this all works properly.
Prefabs, coin, drag a coin just in front of the player there.
Do a big jumping to the coin and one more so we can test that all this works properly.
So at two coins.
So once again, right click create empty call this pick ups.
Reset the transform supporting to keep all these things organized, mostly because your hierarchy doesn't
then get filled up with a whole bunch of gunk, but also so we can do these exact same things like grabbing
the pickups and easily putting them underneath.
Our scene persists, which I haven't put in there yet.
Oh, sorry, I wouldn't put enemies under game session.
What are you doing, Rick?
Something crazy.
So scene persist prefab.
We drag that into the scene.
Sorry, you probably picked that up.
I missed it.
I'm going to grab my enemies and put them where they belong on the scene.
Persist in my pickups under scene.
Persist as well.
Okay sanity check to make sure Rick isn't insane.
Game session canvas.
Yes, that's right.
Same persist.
Enemies and pickups.
Yes, that looks right.
Okay, good one.
Now I'll go all the way back to level one.
So click on levels level one so we can test this from the get go.
I'm just going to move this coin a little bit higher so it's not the way the enemy.
So now that we've got all that structure set up, what I want to do is say when the player reaches the
end of a level or loses all of their lives, I want to have my scene persist, be destroyed, so that
when you're on a new level, that new level scene persist is going to be the boss.
It's going to be controlling things.
So I'm going to give this to you is a little bit more of an abstract challenge.
The code is pretty simple.
We've done this before and we'll be continuing to practice our basic concepts to reinforce them.
And in this case, we're going to create a public method that we can call wherever we need to in order
to reset our scene, persist and then make sure you play test so that you can be sure there are no edge
cases, no weird things.
Like if you pick up three coins when you've got one life and then you die and you go back to that level,
then you go into this level without picking up any coins.
So do a little bit of quality assurance testing on it to make sure you've got it.
So there you go.
There's a challenge plus video.
See you back here when you're done.
Insane persists.
Yes, I mean, sane persists.
What I need to do, first of all, is create public void.
And this I'll call reset scene persist just to be really clear what it is parentheses.
Kelly braces and here's really simply just destroy me destroy game object not magazine Rick but me is
in this particular game object and then save all of that.
So this is really simple code.
There's a lot more nifty ways that you can create this as you become more capable and confident as a
programmer.
But I want to keep practicing these core concepts of if you have something as public within here, we
can call it from somewhere else, and it affects this particular class.
And so where do we need to be calling this public method?
Well, if I go into my game session, this is one place where let's see, let's see.
Not take life.
We don't need to call it there, but in reset game session.
So in other words, when the entire game ends, we absolutely want to be resetting our scene persist.
So just before we load the new scene, I'm going to find object of type.
It will be seen persist parentheses and then we're going to be calling what we'll be calling.
The reset scene persists.
We just created parentheses semicolon.
I'm going to copy that because I'll need it one more time.
Save this script into my explorer.
The other spot I think I need to do this is level exit.
When we leave a level.
So open that up and I'm going to look for where we would do this.
So on trigger, if it's the player, start the routine to load the next level.
We want to wait our little bit of time.
We're waiting in here, but prefer if to do it just before we actually load the new thing.
Let's destroy our scene.
Persist.
So I'll paste that in there under load next level.
After we've done all of our scrutiny and yielding and all that good stuff, we want to find object to
type, scene persist and reset.
Same persist.
So save that up and I'm pretty sure they're the only spots for now.
You might have something in the future where the player can, for example, push a button to exit,
to to reload or a main menu or some other element.
And you need to be thinking of this.
And this is one of the downsides of using this kind of public method where we know in one of our scripts
in here scene persist, we've got this public thing and then go and put it in all of the other places.
Put it here, put it there.
It's really great for us at this beginner plus stage.
It's a nice, simple way of doing things, but there are more detailed ways that you'll learn to do
these things in the future.
That's a little bit more scalable.
But for now, this is a perfectly fine way of doing things for where we're at.
So let's jump in before we congratulate ourselves too much.
Let's make sure it works.
I should have my coins persisting in here when I jump on them, and even when I shoot a goober, they
will go shop one goober, grab one coin, fell down into the hole.
Great.
So those things no longer appear exactly what I wanted.
Now, let's see.
We've got one coin.
One goober will go through the level exit.
That coin is there.
Good.
Grab a few of these coins and then let's just see what happens when we die at this stage.
Great.
Those coins are no longer there.
Let's see what happens when we die.
Now, back to the first one, okay?
And these coins have reappeared.
The group is reappeared.
That means we should have this successfully set up where we have our seen persistence, but only when
we need it.
And just a reminder of why we needed to have that final public method in there.
It's because if this scene persists, is told, you need to stay around forever.
We go into next level and we get to the next level and it says, okay, there's already a scene persist
from the previous level, so I won't bring in my scene persistence than these enemies.
And these pickups might actually not appear in the scene.
So that's why we needed to do that public method.
But I'm feeling pretty good now about our flow and about our features and our functionality, so I'll
see you in the next lecture.