1 00:00:04,040 --> 00:00:04,700 ‫Welcome back. 2 00:00:04,730 --> 00:00:08,220 ‫In this video, I will show you how to get from one scene to another. 3 00:00:08,240 --> 00:00:10,310 ‫So let's create a new scene. 4 00:00:11,120 --> 00:00:12,610 ‫We can do that in multiple ways. 5 00:00:12,620 --> 00:00:15,200 ‫For example, create and scene. 6 00:00:15,200 --> 00:00:17,690 ‫Or we could duplicate our main scene. 7 00:00:17,870 --> 00:00:22,550 ‫And well, let's go ahead and simply create a new scene now. 8 00:00:22,700 --> 00:00:24,140 ‫So create scene. 9 00:00:24,140 --> 00:00:27,470 ‫And I'm going to call that level one. 10 00:00:27,590 --> 00:00:31,160 ‫So now we get from main to, for example, level one. 11 00:00:31,340 --> 00:00:35,660 ‫Let's click on level one and save the current scene that we are at. 12 00:00:36,530 --> 00:00:39,090 ‫And now you can see in this scene there is nothing. 13 00:00:39,110 --> 00:00:47,680 ‫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 14 00:00:47,690 --> 00:00:48,710 ‫which is the text. 15 00:00:48,710 --> 00:00:54,260 ‫And it's just going to say level two. 16 00:00:57,190 --> 00:01:01,600 ‫And I'm going to center that and. 17 00:01:03,160 --> 00:01:04,930 ‫Position it in the center. 18 00:01:05,350 --> 00:01:08,050 ‫Now, I'm going to add a little button here as well. 19 00:01:08,050 --> 00:01:09,670 ‫You I button. 20 00:01:11,430 --> 00:01:21,060 ‫And the text of the button is going to say, move to main scene and that's it. 21 00:01:21,150 --> 00:01:27,060 ‫So what I want to do is I want to get from this scene to the main scene and from the main scene, I 22 00:01:27,060 --> 00:01:29,220 ‫want to get to the level one scene. 23 00:01:29,310 --> 00:01:35,070 ‫So save level one, the scene control SE and then go to your main scene. 24 00:01:35,070 --> 00:01:36,720 ‫And now this button here. 25 00:01:36,990 --> 00:01:40,230 ‫What it should do is move us to level one. 26 00:01:41,010 --> 00:01:43,380 ‫Go to level one. 27 00:01:44,310 --> 00:01:44,970 ‫All right. 28 00:01:46,890 --> 00:01:49,170 ‫Now the button already has a behavior. 29 00:01:49,170 --> 00:01:51,090 ‫So this one has a behavior already. 30 00:01:51,090 --> 00:01:52,200 ‫So let's change that. 31 00:01:52,200 --> 00:01:53,850 ‫Let's go to button behavior. 32 00:01:54,240 --> 00:02:00,450 ‫And what I want to do here is I want to change the on button pressed here. 33 00:02:01,440 --> 00:02:05,430 ‫I don't want to set the text of our score. 34 00:02:05,460 --> 00:02:12,960 ‫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 35 00:02:12,960 --> 00:02:13,650 ‫as well. 36 00:02:13,650 --> 00:02:19,290 ‫And while we're at it, we can also get rid of Unity Engine because we're not using it right now. 37 00:02:19,950 --> 00:02:26,160 ‫What I will need, however, is the scene manager, which allows us to move from one scene to another. 38 00:02:26,160 --> 00:02:37,140 ‫So let's go ahead and use the unity engine scene management, and that one contains the scene manager 39 00:02:37,170 --> 00:02:43,620 ‫because only if you add this namespace, you will be you will be able to use the scene manager. 40 00:02:43,860 --> 00:02:45,810 ‫So let's open up the scene manager. 41 00:02:45,930 --> 00:02:49,560 ‫And that scene manager has multiple different methods. 42 00:02:49,560 --> 00:02:54,630 ‫And the one that is important for us currently is the load scene method. 43 00:02:54,930 --> 00:02:59,250 ‫What that will do is it will allow us to move from one scene to another. 44 00:02:59,250 --> 00:03:05,730 ‫And as you can see, it has multiple overloads, it has an int scene build index, it has a string C 45 00:03:05,730 --> 00:03:09,030 ‫name and it also has some other options. 46 00:03:09,030 --> 00:03:15,810 ‫For example, this load scene mode argument, we're just going to use the one where we can simply add 47 00:03:15,810 --> 00:03:19,110 ‫the index of the scene that we want to go to. 48 00:03:19,140 --> 00:03:26,550 ‫So let's say I want to go to scene one or I could go to scene level one. 49 00:03:27,120 --> 00:03:28,350 ‫So let's save that. 50 00:03:28,350 --> 00:03:35,160 ‫Let's have a look if that's the right name actually is level without empty space one. 51 00:03:35,370 --> 00:03:37,830 ‫So let's make sure that this is the right name. 52 00:03:38,010 --> 00:03:39,180 ‫Let's save that. 53 00:03:39,180 --> 00:03:44,550 ‫And now whenever somebody presses on this button, he should go from main to level one. 54 00:03:44,550 --> 00:03:45,780 ‫So let's test that. 55 00:03:45,930 --> 00:03:49,410 ‫We will actually run into an error first, but let's see what happens. 56 00:03:49,410 --> 00:03:55,830 ‫So I run, I run it and I press on, go to level one and I get a an error here. 57 00:03:55,860 --> 00:03:59,010 ‫It says stop the game scene. 58 00:03:59,010 --> 00:04:04,410 ‫Level one could not be loaded because it has not been added to the build settings or the asset build 59 00:04:04,410 --> 00:04:05,010 ‫bundle. 60 00:04:05,010 --> 00:04:09,510 ‫And if ever, if you ever see this error, there is one very simple solution. 61 00:04:09,510 --> 00:04:16,980 ‫You go to file, build settings, and in here you add the scenes that you want to have in there. 62 00:04:16,980 --> 00:04:21,540 ‫So you add scene main and you add scene level one. 63 00:04:22,290 --> 00:04:23,760 ‫Now you can close that up. 64 00:04:23,760 --> 00:04:27,420 ‫By the way, what you should memorize is the zero in this one. 65 00:04:27,420 --> 00:04:30,930 ‫So main is zero and level one is one. 66 00:04:31,020 --> 00:04:33,330 ‫Actually, let's make sure that it's like that. 67 00:04:33,330 --> 00:04:39,240 ‫So here the index, the scene index, that's this number here at the right hand side. 68 00:04:39,810 --> 00:04:46,470 ‫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 69 00:04:46,470 --> 00:04:48,420 ‫to our level. 70 00:04:48,420 --> 00:04:52,140 ‫One scene, by the way, here on the right hand side, you can see that we are currently in main. 71 00:04:52,140 --> 00:04:53,610 ‫So let's click on that button. 72 00:04:53,610 --> 00:04:56,820 ‫And now you can see we are in a scene level one. 73 00:04:56,820 --> 00:05:01,200 ‫At the same time you see that the text has changed and now it says move to main scene. 74 00:05:01,200 --> 00:05:08,490 ‫So this is a different scene that we are at and you can use that to, for example, move from from the 75 00:05:08,490 --> 00:05:11,190 ‫start screen where the player can simply start the game. 76 00:05:11,190 --> 00:05:19,440 ‫We can see the high score and maybe change the settings to the game scene itself or to a specific level. 77 00:05:19,620 --> 00:05:25,110 ‫You could even build, for example, 20 different buttons here and button one is level one, button 78 00:05:25,110 --> 00:05:27,390 ‫two is level two and so forth. 79 00:05:27,930 --> 00:05:32,430 ‫So now if we press move to main scene, of course nothing happens. 80 00:05:32,580 --> 00:05:39,480 ‫So for you as a little challenge, please go ahead and try to add the functionality to our level one 81 00:05:39,480 --> 00:05:44,430 ‫scene so that you can move from this scene to our main scene. 82 00:05:45,060 --> 00:05:46,590 ‫So pause the video and try that. 83 00:05:48,950 --> 00:05:49,310 ‫All right. 84 00:05:49,310 --> 00:05:50,390 ‫I hope you tried it. 85 00:05:50,480 --> 00:05:52,190 ‫So let's go ahead and do that. 86 00:05:53,060 --> 00:05:56,240 ‫And there are multiple different ways. 87 00:05:56,600 --> 00:06:02,030 ‫For example, you could create a very new script and you could assign that script to your button, and 88 00:06:02,030 --> 00:06:05,320 ‫then you could move from this screen to another screen. 89 00:06:05,330 --> 00:06:08,420 ‫But I'm going to show you a cleaner way, let's say. 90 00:06:08,450 --> 00:06:10,340 ‫So let's go to button behavior. 91 00:06:10,430 --> 00:06:15,770 ‫And now we go ahead and change that from on button press, which is the name that I have just used in 92 00:06:15,770 --> 00:06:20,450 ‫order for you to see that this was called When We Press a Button. 93 00:06:20,450 --> 00:06:26,600 ‫But I actually want to have a method that has a better name, which explains a bit better what should 94 00:06:26,600 --> 00:06:27,170 ‫happen. 95 00:06:27,170 --> 00:06:34,400 ‫So the method should not be called on button press this time, but move to scene. 96 00:06:35,510 --> 00:06:38,450 ‫So now this method will move from one scene to another. 97 00:06:38,450 --> 00:06:47,600 ‫And as an argument I'm going to add an integer which I'm going to call scene ID and now we load the 98 00:06:47,600 --> 00:06:49,490 ‫scene and we don't load level one. 99 00:06:49,490 --> 00:06:56,570 ‫But what we do is we load the scene ID, All right. 100 00:06:56,570 --> 00:07:01,040 ‫And now what we can do is we can add that to our button. 101 00:07:01,370 --> 00:07:05,270 ‫Let's add the button behavior to our button object. 102 00:07:05,600 --> 00:07:08,180 ‫So script button behavior. 103 00:07:08,180 --> 00:07:11,540 ‫And now we can add an on click event here listing. 104 00:07:11,540 --> 00:07:16,640 ‫Next, we need to drag the button behavior in here so you can see it has been added. 105 00:07:16,640 --> 00:07:20,060 ‫Now we can choose the button behavior. 106 00:07:22,570 --> 00:07:24,520 ‫And their move to scene. 107 00:07:25,120 --> 00:07:28,420 ‫Now it expects an ENT, which is this number here. 108 00:07:28,420 --> 00:07:34,650 ‫So it's currently number zero, which is in fact the scene that we want to go to. 109 00:07:34,660 --> 00:07:43,270 ‫So if we go to build settings, we can see the main scene has the index zero, the scene index zero, 110 00:07:43,270 --> 00:07:45,460 ‫which means that it's the right scene. 111 00:07:46,060 --> 00:07:46,390 ‫All right. 112 00:07:46,390 --> 00:07:49,960 ‫So let's go ahead and go to our main scene as well. 113 00:07:49,990 --> 00:07:58,990 ‫In here we have our canvas and the button and now the button is missing the on button press method. 114 00:07:58,990 --> 00:08:01,900 ‫Of course, he's missing it because it doesn't exist anymore. 115 00:08:01,900 --> 00:08:03,040 ‫I changed the name of it. 116 00:08:03,040 --> 00:08:07,660 ‫So whenever you do that, you have to be super careful because in here it has not been changed. 117 00:08:07,810 --> 00:08:18,220 ‫So what I'm doing now is I'm choosing the move to scene and here I enter one because our level one. 118 00:08:20,060 --> 00:08:22,850 ‫Scene has the index of one. 119 00:08:23,000 --> 00:08:30,050 ‫So now I'm going to move from zero, from scene zero, which is our main scene to scene one, which 120 00:08:30,050 --> 00:08:31,370 ‫is our level one scene. 121 00:08:31,670 --> 00:08:33,230 ‫So let's test that. 122 00:08:37,020 --> 00:08:38,520 ‫Now let's go to level one. 123 00:08:38,910 --> 00:08:39,960 ‫Let's press on it. 124 00:08:40,110 --> 00:08:43,170 ‫You can see we are in our level one scene. 125 00:08:43,440 --> 00:08:49,080 ‫Now let's press move to main scene and we see we go back to our level one scene. 126 00:08:49,590 --> 00:08:49,980 ‫All right. 127 00:08:49,980 --> 00:08:57,570 ‫So now you have seen how you can use methods and buttons in order to move from one scene to another. 128 00:08:57,720 --> 00:09:01,230 ‫Of course, you can call the scene manager. 129 00:09:01,230 --> 00:09:08,370 ‫So this code here, at any point, you could, for example, say as soon as the player has lost or as 130 00:09:08,370 --> 00:09:15,210 ‫soon as the player has died, please load the game over scene or as soon as the player has reached the 131 00:09:15,210 --> 00:09:17,910 ‫end of this level, go to the next level. 132 00:09:17,910 --> 00:09:24,000 ‫So that could be done dynamically and not by user input as it is currently with the buttons. 133 00:09:24,000 --> 00:09:28,710 ‫But I just wanted to show you how it's possible to do it via via buttons. 134 00:09:29,190 --> 00:09:35,460 ‫That's it for buttons and their click events and also for moving from scene to scene be.