1 00:00:03,950 --> 00:00:04,970 ‫Welcome back. 2 00:00:05,000 --> 00:00:12,350 ‫Currently, we have the behavior in our game that whenever we hit one bomb or any bomb, then the game 3 00:00:12,350 --> 00:00:13,160 ‫just stops. 4 00:00:13,160 --> 00:00:16,820 ‫And that's not really the best behavior we want to have. 5 00:00:17,000 --> 00:00:21,590 ‫The one that we want to have is, of course, a game over screen or something like that. 6 00:00:21,590 --> 00:00:26,210 ‫And in our case, I think the best choice is to stop the game in the background and at the same time 7 00:00:26,210 --> 00:00:32,960 ‫open up a panel which bill will display that is game over and will allow us to restart the game. 8 00:00:33,260 --> 00:00:34,880 ‫That's what we're going to build in this video. 9 00:00:34,880 --> 00:00:38,120 ‫So let's go ahead and create a panel now. 10 00:00:38,120 --> 00:00:39,230 ‫I don't want to stretch it. 11 00:00:39,230 --> 00:00:47,270 ‫I want to center the panel and let's make it 500 wide and maybe 250 high. 12 00:00:47,300 --> 00:00:49,250 ‫So that's going to be our panel now. 13 00:00:49,250 --> 00:00:55,910 ‫And let's add a text into the panel UI text that will be inside of the panel. 14 00:00:55,940 --> 00:00:59,840 ‫Let's center that in both directions. 15 00:00:59,840 --> 00:01:05,960 ‫Then let's maybe change well, let's position it towards the top of the panel. 16 00:01:06,140 --> 00:01:08,060 ‫And now let's set zero. 17 00:01:08,240 --> 00:01:09,140 ‫Set that to zero. 18 00:01:09,140 --> 00:01:12,140 ‫Actually, let's drag that down a little. 19 00:01:14,270 --> 00:01:21,860 ‫It's best maybe in the scene view where actually let's change the value a little bit to let's say -30 20 00:01:22,070 --> 00:01:23,180 ‫or 40. 21 00:01:23,780 --> 00:01:24,960 ‫I think that should be fine. 22 00:01:24,980 --> 00:01:31,910 ‫Now let's change the font size to something like 48. 23 00:01:31,940 --> 00:01:34,900 ‫Now, of course, we don't see it because the height is only 30. 24 00:01:34,910 --> 00:01:39,320 ‫So what we can do is overflow vertically and horizontally. 25 00:01:39,320 --> 00:01:48,020 ‫Now we see that and that should show game over and maybe even with an empty space in between. 26 00:01:48,020 --> 00:01:49,640 ‫So the panel is not a panel anymore. 27 00:01:49,640 --> 00:01:51,470 ‫It's a game over panel now. 28 00:01:51,500 --> 00:01:53,360 ‫So let's name it that way. 29 00:01:53,360 --> 00:01:58,760 ‫And the text should maybe also be game over text. 30 00:02:01,040 --> 00:02:01,460 ‫All right. 31 00:02:01,460 --> 00:02:07,010 ‫So now that's the panel and we will activate or deactivate the panel based on the game state. 32 00:02:07,010 --> 00:02:10,100 ‫So if it's game over, we want to see the game over panel. 33 00:02:10,100 --> 00:02:13,340 ‫And if it's not game over, then we, of course, don't want to see it. 34 00:02:13,910 --> 00:02:14,180 ‫All right. 35 00:02:14,180 --> 00:02:21,170 ‫So let's take care of the activating and and activating it in our game manager script. 36 00:02:21,170 --> 00:02:23,180 ‫So let's open up our game manager. 37 00:02:23,510 --> 00:02:28,700 ‫And in here currently we only have increased score and we have our on bump head and what we will need 38 00:02:28,730 --> 00:02:31,280 ‫is a new game object. 39 00:02:31,280 --> 00:02:37,100 ‫So I'm going to call that public game object game over panel. 40 00:02:37,610 --> 00:02:39,380 ‫So that's going to be our game over panel. 41 00:02:39,380 --> 00:02:44,420 ‫And while we're at it, let's make it a little nicer by adding a header. 42 00:02:45,110 --> 00:02:47,450 ‫This one will be score elements. 43 00:02:51,110 --> 00:02:58,460 ‫And that one here will be header game over. 44 00:03:00,620 --> 00:03:02,150 ‫You will see what that means in a second. 45 00:03:02,150 --> 00:03:07,010 ‫So let's go there and go to our game manager and in a second it will change. 46 00:03:07,010 --> 00:03:08,540 ‫So it needs to update. 47 00:03:10,280 --> 00:03:11,750 ‫And as you can see, score elements. 48 00:03:11,750 --> 00:03:12,530 ‫That's the header. 49 00:03:12,530 --> 00:03:18,500 ‫So this bold black text here, score element and that's the other header called Game Over. 50 00:03:18,500 --> 00:03:20,090 ‫Now we need game over panel. 51 00:03:20,090 --> 00:03:20,960 ‫So let's get that. 52 00:03:20,960 --> 00:03:22,640 ‫Let's drag it right in there. 53 00:03:22,640 --> 00:03:30,260 ‫And what we want to do in our game manager is to deactivate our game over panel whenever we start the 54 00:03:30,260 --> 00:03:30,860 ‫game. 55 00:03:30,860 --> 00:03:33,140 ‫So it's best to do that in a wake. 56 00:03:33,140 --> 00:03:44,300 ‫So there we set the game over panel to set active of on to false so what that does it says okay set 57 00:03:44,300 --> 00:03:48,770 ‫it to active but actually set it false active which is deactivated. 58 00:03:48,770 --> 00:03:51,950 ‫So the game over panel will be deactivated on a wake. 59 00:03:51,950 --> 00:03:53,960 ‫So let's quickly check that out. 60 00:03:53,960 --> 00:03:55,040 ‫We start the game. 61 00:03:55,760 --> 00:03:56,090 ‫All right. 62 00:03:56,090 --> 00:03:56,750 ‫So try that. 63 00:03:56,750 --> 00:04:01,550 ‫Let's try that play and we see it disappears. 64 00:04:01,580 --> 00:04:02,300 ‫Great. 65 00:04:04,750 --> 00:04:06,880 ‫So when do we want it to appear? 66 00:04:06,970 --> 00:04:09,640 ‫Well, actually, whenever we stop the game. 67 00:04:09,640 --> 00:04:11,770 ‫So here, game over, panel. 68 00:04:12,400 --> 00:04:13,610 ‫Set active. 69 00:04:13,630 --> 00:04:14,260 ‫True. 70 00:04:14,800 --> 00:04:19,600 ‫So that will allow us to see the game over area. 71 00:04:19,600 --> 00:04:24,490 ‫So this whole panel, with all its information, whenever we lose the game. 72 00:04:24,490 --> 00:04:25,600 ‫So let's get there. 73 00:04:25,660 --> 00:04:27,100 ‫Let's try to hit the bump. 74 00:04:27,730 --> 00:04:28,980 ‫And I hit the bump. 75 00:04:28,990 --> 00:04:30,180 ‫The game was paused. 76 00:04:30,190 --> 00:04:31,660 ‫We can see it in the background. 77 00:04:31,660 --> 00:04:32,410 ‫It's very nice. 78 00:04:32,410 --> 00:04:35,770 ‫And we have this little area here which says Game over. 79 00:04:35,800 --> 00:04:40,660 ‫Now, of course, we need a button on it and some further objects as well. 80 00:04:42,370 --> 00:04:45,130 ‫For example, the score would be nice to see here. 81 00:04:45,130 --> 00:04:47,290 ‫So let's go ahead and add the score. 82 00:04:47,890 --> 00:04:57,310 ‫And by that, I mean let's duplicate the game over text and changed it to score text and let's drag 83 00:04:57,310 --> 00:05:03,520 ‫the score text further down to -100 maybe. 84 00:05:03,820 --> 00:05:04,250 ‫Yeah. 85 00:05:04,570 --> 00:05:07,870 ‫Let's reduce the font size to 30. 86 00:05:07,900 --> 00:05:08,740 ‫Let's see how that looks. 87 00:05:08,740 --> 00:05:11,530 ‫Yeah, that's already quite good. 88 00:05:11,710 --> 00:05:14,740 ‫Maybe 100, maybe -80. 89 00:05:15,340 --> 00:05:16,360 ‫It should be better. 90 00:05:16,360 --> 00:05:20,530 ‫And the text should say something like score zero. 91 00:05:21,670 --> 00:05:22,090 ‫All right. 92 00:05:22,090 --> 00:05:25,000 ‫But then we want to do that programmatically, of course. 93 00:05:25,000 --> 00:05:31,810 ‫So in here, what we'll need is to set the text of our game over. 94 00:05:32,690 --> 00:05:36,410 ‫Panel cortex actually will need a new variable for that. 95 00:05:36,410 --> 00:05:37,490 ‫So let's create one. 96 00:05:37,910 --> 00:05:39,410 ‫Let's call it 97 00:05:41,900 --> 00:05:43,820 ‫public text. 98 00:05:44,480 --> 00:05:47,780 ‫Game over panel, score text. 99 00:05:47,780 --> 00:05:50,540 ‫Quite a long name, but it's very descriptive. 100 00:05:51,260 --> 00:05:58,280 ‫And then we use that game over panel score text in order to set the score 101 00:06:00,920 --> 00:06:01,750 ‫into it. 102 00:06:01,760 --> 00:06:03,440 ‫So score to string. 103 00:06:04,910 --> 00:06:06,770 ‫All right, so let's try that. 104 00:06:08,360 --> 00:06:11,340 ‫And actually we shouldn't try that because we need to assign it. 105 00:06:11,360 --> 00:06:18,110 ‫So let's get to a game manager and drag our score text into the game manager here. 106 00:06:18,470 --> 00:06:20,350 ‫So not the one from the canvas directory. 107 00:06:20,360 --> 00:06:27,740 ‫Maybe we should even rename it to game over label score text. 108 00:06:29,690 --> 00:06:30,890 ‫All right, it's in there. 109 00:06:30,890 --> 00:06:34,940 ‫And now let's try to hit one point and at the same time hit the bump. 110 00:06:35,150 --> 00:06:37,940 ‫And as you can see, three is a text here. 111 00:06:37,970 --> 00:06:42,020 ‫Now, of course, we want to have something like score zero or score three there. 112 00:06:42,020 --> 00:06:44,630 ‫So we need to edit it a little bit. 113 00:06:44,990 --> 00:06:48,980 ‫So let's go in here and change that to score. 114 00:06:49,550 --> 00:06:52,580 ‫Colon, empty space, plus the score. 115 00:06:53,150 --> 00:06:53,450 ‫All right. 116 00:06:53,450 --> 00:06:54,590 ‫So that will fix it. 117 00:06:54,830 --> 00:06:56,690 ‫We can quickly check that again. 118 00:06:56,690 --> 00:07:00,200 ‫I'm trying to bomp and score at the same time. 119 00:07:01,520 --> 00:07:04,250 ‫So score and bom and we see score three. 120 00:07:05,060 --> 00:07:07,490 ‫Next we want to add a restart button in here. 121 00:07:07,490 --> 00:07:09,080 ‫So let's go ahead and do that. 122 00:07:09,110 --> 00:07:16,550 ‫Create UI because that button will allow us to restart the game and then we can go ahead and play some 123 00:07:16,550 --> 00:07:17,090 ‫more. 124 00:07:17,180 --> 00:07:20,900 ‫So let's change the position just a little bit, actually. 125 00:07:22,070 --> 00:07:22,400 ‫Let's go. 126 00:07:22,430 --> 00:07:23,810 ‫-50. 127 00:07:24,290 --> 00:07:25,340 ‫That should be fine. 128 00:07:25,670 --> 00:07:29,570 ‫Now let's change the text to restart. 129 00:07:30,530 --> 00:07:35,620 ‫And what I want to do is I want to set this time scale on one whenever somebody pressed the button. 130 00:07:35,630 --> 00:07:45,440 ‫So let's play voit restart game and I set the time scale 131 00:07:47,960 --> 00:07:48,680 ‫to one. 132 00:07:49,790 --> 00:07:51,100 ‫So let's test that. 133 00:07:51,110 --> 00:07:52,940 ‫Let's see if that button works. 134 00:07:52,970 --> 00:07:53,230 ‫Okay. 135 00:07:53,330 --> 00:07:55,030 ‫So now we, of course, need the button. 136 00:07:55,040 --> 00:07:57,800 ‫So let's go ahead and go to the restart button. 137 00:07:59,210 --> 00:08:04,150 ‫And in here, let's add the on click event that's drag the game manager in here. 138 00:08:04,160 --> 00:08:09,200 ‫So now we will have the functionality of the game manager, which is, for example, restart game. 139 00:08:10,130 --> 00:08:15,650 ‫So what that will do is it will restart the game as we've just seen, or at least in this case it will 140 00:08:15,650 --> 00:08:18,890 ‫set the speed to or a time scale to one again. 141 00:08:19,040 --> 00:08:20,510 ‫So let's try that. 142 00:08:22,250 --> 00:08:24,590 ‫Let's hit the bump and let's press restart. 143 00:08:24,590 --> 00:08:29,600 ‫And you can see that the game goes on in the background until we hit the bump again. 144 00:08:30,080 --> 00:08:35,330 ‫And of course, we want not only to do that, we also want to restart the game in terms of getting rid 145 00:08:35,330 --> 00:08:38,600 ‫of all the game objects and resetting the score. 146 00:08:39,380 --> 00:08:39,650 ‫All right. 147 00:08:39,650 --> 00:08:41,510 ‫So let's go back to the script. 148 00:08:41,510 --> 00:08:46,130 ‫And in the restart game method, we all need to set the score back to zero. 149 00:08:46,820 --> 00:08:50,240 ‫So as soon as we restart the game, there are multiple things of course we need to do. 150 00:08:50,240 --> 00:08:57,830 ‫Reset the score, set the score, text back to zero D, activate the panel, then set the text of the 151 00:08:57,830 --> 00:09:02,060 ‫panel back to zero and destroy all the game objects. 152 00:09:02,570 --> 00:09:03,650 ‫So let's do that. 153 00:09:03,890 --> 00:09:05,660 ‫Score is zero. 154 00:09:05,660 --> 00:09:06,670 ‫Score text. 155 00:09:06,710 --> 00:09:09,950 ‫Text is set to zero as well. 156 00:09:10,880 --> 00:09:16,940 ‫Then our game over panel should be deactivated. 157 00:09:16,940 --> 00:09:26,840 ‫So it's set to false and our game over panel score text can be set back to score colon zero. 158 00:09:27,290 --> 00:09:33,920 ‫Now finally, we need to destroy all the game objects in our game, at least the ones that are interacting 159 00:09:34,310 --> 00:09:35,000 ‫with. 160 00:09:35,000 --> 00:09:36,520 ‫And we can do that the following way. 161 00:09:36,530 --> 00:09:37,820 ‫Game object. 162 00:09:38,450 --> 00:09:43,220 ‫I'm going to call a G in game object, find 163 00:09:45,590 --> 00:09:50,420 ‫game object with tag and actually the plural game objects with tag. 164 00:09:50,660 --> 00:09:56,120 ‫And now we need to enter a tag and I'm going to use the tag enter EC to bowl. 165 00:09:57,920 --> 00:10:02,900 ‫And for all the objects that are intractable, I want to destroy them. 166 00:10:03,200 --> 00:10:04,710 ‫So what does that mean? 167 00:10:04,730 --> 00:10:08,270 ‫Well, let's have a look in unity, then we'll make that will make sense. 168 00:10:08,450 --> 00:10:16,800 ‫So our prefabs, in our case, the Bomp and the orange and the orange cut, they all need a tag. 169 00:10:16,820 --> 00:10:24,380 ‫Let's add a tag by going to untagged here, then add tag and then pressing this little plus button here 170 00:10:24,380 --> 00:10:26,570 ‫that will allow us to add a new tag to our list. 171 00:10:26,570 --> 00:10:29,870 ‫And I'm going to call it Enter to Bowl. 172 00:10:32,590 --> 00:10:35,710 ‫Save that and now I need to assign that to our bomp. 173 00:10:36,760 --> 00:10:37,990 ‫And what else is interactive? 174 00:10:38,170 --> 00:10:39,580 ‫Well, our orange. 175 00:10:41,230 --> 00:10:43,390 ‫And our orange cut. 176 00:10:45,170 --> 00:10:47,750 ‫So that bond here is intractable as well. 177 00:10:47,780 --> 00:10:48,260 ‫All right. 178 00:10:48,260 --> 00:10:52,910 ‫And the orange is so as well, because the prefabs are so now. 179 00:10:54,590 --> 00:10:55,610 ‫Whenever. 180 00:10:56,490 --> 00:10:57,750 ‫I restart the game. 181 00:10:57,780 --> 00:11:02,470 ‫Every single object that has the tag intractable will be destroyed. 182 00:11:02,490 --> 00:11:05,450 ‫That's what this code here says, those three lines. 183 00:11:05,460 --> 00:11:11,310 ‫So it loops through all the game objects in our game which have this tag and then it destroys them. 184 00:11:12,300 --> 00:11:13,890 ‫All right, so let's try that. 185 00:11:14,010 --> 00:11:15,720 ‫Let's see what happens. 186 00:11:17,610 --> 00:11:22,440 ‫So I'm going to play a little bit, get some points until the bomb comes. 187 00:11:23,520 --> 00:11:26,070 ‫And I want to hit the first bomb that comes up. 188 00:11:29,580 --> 00:11:30,840 ‫And there it is. 189 00:11:30,870 --> 00:11:31,660 ‫I hit the bump. 190 00:11:31,680 --> 00:11:32,940 ‫Now let's press restart. 191 00:11:33,570 --> 00:11:35,790 ‫And we see all the objects were destroyed. 192 00:11:36,000 --> 00:11:40,740 ‫Our score was set to zero and we are back in the game. 193 00:11:40,980 --> 00:11:44,200 ‫Now, let's have a look here at the right hand side at the hierarchy. 194 00:11:44,220 --> 00:11:44,940 ‫Be careful. 195 00:11:44,940 --> 00:11:48,060 ‫I press restart and you can see all of them are destroyed. 196 00:11:48,060 --> 00:11:50,490 ‫And the new ones, of course, spawn directly. 197 00:11:51,690 --> 00:11:52,430 ‫All right. 198 00:11:52,440 --> 00:11:59,130 ‫Now we know how to start the game, how to restart the game, and how to get rid of all the objects 199 00:11:59,130 --> 00:12:03,090 ‫in our game, reset the whole state, pause the game and so forth. 200 00:12:03,690 --> 00:12:07,980 ‫In the next video, we are going to add the high score to our game. 201 00:12:07,980 --> 00:12:11,490 ‫So we have this best zero, but we don't save anything yet. 202 00:12:11,490 --> 00:12:12,450 ‫So let's do that.