Welcome back.
In this video, I will show you how to get from one scene to another.
So let's create a new scene.
We can do that in multiple ways.
For example, create and scene.
Or we could duplicate our main scene.
And well, let's go ahead and simply create a new scene now.
So create scene.
And I'm going to call that level one.
So now we get from main to, for example, level one.
Let's click on level one and save the current scene that we are at.
And now you can see in this scene there is nothing.
So the only thing that I'm going to add to our level one scene is a UI and UI, which I call text or
which is the text.
And it's just going to say level two.
And I'm going to center that and.
Position it in the center.
Now, I'm going to add a little button here as well.
You I button.
And the text of the button is going to say, move to main scene and that's it.
So what I want to do is I want to get from this scene to the main scene and from the main scene, I
want to get to the level one scene.
So save level one, the scene control SE and then go to your main scene.
And now this button here.
What it should do is move us to level one.
Go to level one.
All right.
Now the button already has a behavior.
So this one has a behavior already.
So let's change that.
Let's go to button behavior.
And what I want to do here is I want to change the on button pressed here.
I don't want to set the text of our score.
I want to move from one scene to another so I can get rid of this text and I can get rid of this line
as well.
And while we're at it, we can also get rid of Unity Engine because we're not using it right now.
What I will need, however, is the scene manager, which allows us to move from one scene to another.
So let's go ahead and use the unity engine scene management, and that one contains the scene manager
because only if you add this namespace, you will be you will be able to use the scene manager.
So let's open up the scene manager.
And that scene manager has multiple different methods.
And the one that is important for us currently is the load scene method.
What that will do is it will allow us to move from one scene to another.
And as you can see, it has multiple overloads, it has an int scene build index, it has a string C
name and it also has some other options.
For example, this load scene mode argument, we're just going to use the one where we can simply add
the index of the scene that we want to go to.
So let's say I want to go to scene one or I could go to scene level one.
So let's save that.
Let's have a look if that's the right name actually is level without empty space one.
So let's make sure that this is the right name.
Let's save that.
And now whenever somebody presses on this button, he should go from main to level one.
So let's test that.
We will actually run into an error first, but let's see what happens.
So I run, I run it and I press on, go to level one and I get a an error here.
It says stop the game scene.
Level one could not be loaded because it has not been added to the build settings or the asset build
bundle.
And if ever, if you ever see this error, there is one very simple solution.
You go to file, build settings, and in here you add the scenes that you want to have in there.
So you add scene main and you add scene level one.
Now you can close that up.
By the way, what you should memorize is the zero in this one.
So main is zero and level one is one.
Actually, let's make sure that it's like that.
So here the index, the scene index, that's this number here at the right hand side.
So now let's close it and let's run the code again and see if we can move from scene from our main scene
to our level.
One scene, by the way, here on the right hand side, you can see that we are currently in main.
So let's click on that button.
And now you can see we are in a scene level one.
At the same time you see that the text has changed and now it says move to main scene.
So this is a different scene that we are at and you can use that to, for example, move from from the
start screen where the player can simply start the game.
We can see the high score and maybe change the settings to the game scene itself or to a specific level.
You could even build, for example, 20 different buttons here and button one is level one, button
two is level two and so forth.
So now if we press move to main scene, of course nothing happens.
So for you as a little challenge, please go ahead and try to add the functionality to our level one
scene so that you can move from this scene to our main scene.
So pause the video and try that.
All right.
I hope you tried it.
So let's go ahead and do that.
And there are multiple different ways.
For example, you could create a very new script and you could assign that script to your button, and
then you could move from this screen to another screen.
But I'm going to show you a cleaner way, let's say.
So let's go to button behavior.
And now we go ahead and change that from on button press, which is the name that I have just used in
order for you to see that this was called When We Press a Button.
But I actually want to have a method that has a better name, which explains a bit better what should
happen.
So the method should not be called on button press this time, but move to scene.
So now this method will move from one scene to another.
And as an argument I'm going to add an integer which I'm going to call scene ID and now we load the
scene and we don't load level one.
But what we do is we load the scene ID, All right.
And now what we can do is we can add that to our button.
Let's add the button behavior to our button object.
So script button behavior.
And now we can add an on click event here listing.
Next, we need to drag the button behavior in here so you can see it has been added.
Now we can choose the button behavior.
And their move to scene.
Now it expects an ENT, which is this number here.
So it's currently number zero, which is in fact the scene that we want to go to.
So if we go to build settings, we can see the main scene has the index zero, the scene index zero,
which means that it's the right scene.
All right.
So let's go ahead and go to our main scene as well.
In here we have our canvas and the button and now the button is missing the on button press method.
Of course, he's missing it because it doesn't exist anymore.
I changed the name of it.
So whenever you do that, you have to be super careful because in here it has not been changed.
So what I'm doing now is I'm choosing the move to scene and here I enter one because our level one.
Scene has the index of one.
So now I'm going to move from zero, from scene zero, which is our main scene to scene one, which
is our level one scene.
So let's test that.
Now let's go to level one.
Let's press on it.
You can see we are in our level one scene.
Now let's press move to main scene and we see we go back to our level one scene.
All right.
So now you have seen how you can use methods and buttons in order to move from one scene to another.
Of course, you can call the scene manager.
So this code here, at any point, you could, for example, say as soon as the player has lost or as
soon as the player has died, please load the game over scene or as soon as the player has reached the
end of this level, go to the next level.
So that could be done dynamically and not by user input as it is currently with the buttons.
But I just wanted to show you how it's possible to do it via via buttons.
That's it for buttons and their click events and also for moving from scene to scene be.