Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
1
00:00:00,000 --> 00:00:04,207
[MUSIC]
2
2
00:00:04,207 --> 00:00:08,093
In this video we're going to hook it up so
that when all the blocks are destroyed,
3
3
00:00:08,093 --> 00:00:09,245
we load our next scene.
4
4
00:00:09,245 --> 00:00:12,005
Super exciting, so let's get started.
5
5
00:00:12,005 --> 00:00:15,990
One thing I didn't talk about fully in the
previous video that I'll touch on now is
6
6
00:00:15,990 --> 00:00:20,177
each of these blocks, you've got one, two,
three, all the way down to ten blocks.
7
7
00:00:20,177 --> 00:00:23,932
Each of these blocks
contains the script block.
8
8
00:00:23,932 --> 00:00:28,914
And on that script block we have,
we're saying, we're calling the method,
9
9
00:00:28,914 --> 00:00:34,288
CountBreakableBlock which basically just
increments the number of blocks by one.
10
10
00:00:34,288 --> 00:00:38,123
And the reason that adds up the total
number of blocks is when we start
11
11
00:00:38,123 --> 00:00:42,365
the game, every single one of these
instances, these are instances of our
12
12
00:00:42,365 --> 00:00:46,365
prefab, every instance says,
do the thing that is on my script.
13
13
00:00:46,365 --> 00:00:47,785
So count me as a block.
14
14
00:00:47,785 --> 00:00:49,718
And then this one says, add another block.
15
15
00:00:49,718 --> 00:00:53,532
And this one says, add another block,
add another block.
16
16
00:00:53,532 --> 00:00:55,566
So each of these are saying
add another block.
17
17
00:00:55,566 --> 00:00:57,732
And so they're doing that ten times.
18
18
00:00:57,732 --> 00:01:00,449
That's why it's adding up in our level.
19
19
00:01:00,449 --> 00:01:02,532
It's adding up all the breakable blocks.
20
20
00:01:02,532 --> 00:01:06,379
Each of them have said count me,
and that adds up to ten.
21
21
00:01:06,379 --> 00:01:09,767
Okay, so just wanted to be super
clear with what's going on there.
22
22
00:01:09,767 --> 00:01:12,556
Now we're gonna jump into level.cs.
23
23
00:01:12,556 --> 00:01:16,099
Again, we need to create
ourselves our next public method.
24
24
00:01:16,099 --> 00:01:19,732
Cuz if we look at our flow we can
see that we have gone through,
25
25
00:01:19,732 --> 00:01:24,298
we've created a level object,
we're keeping track of breakable blocks,
26
26
00:01:24,298 --> 00:01:26,103
we're adding up the blocks.
27
27
00:01:26,103 --> 00:01:28,989
Now we need a method to signal
that a block was broken,
28
28
00:01:28,989 --> 00:01:32,388
an if statement to load the next
level if the blocks is zero, and
29
29
00:01:32,388 --> 00:01:36,072
then also to tell each block to
remove itself when it's destroyed.
30
30
00:01:36,072 --> 00:01:39,581
Remove itself from the total
number of breakable blocks, and
31
31
00:01:39,581 --> 00:01:41,884
that's what we're up to at the moment.
32
32
00:01:41,884 --> 00:01:45,904
The first thing we'll do is create
a public method, public void, and
33
33
00:01:45,904 --> 00:01:48,163
we'll call this one block destroyed.
34
34
00:01:48,163 --> 00:01:49,945
Block destroyed.
35
35
00:01:49,945 --> 00:01:54,086
I have an enclosed parenthesis,
I have enclosed curly braces brackets,
36
36
00:01:54,086 --> 00:01:56,087
and then within here we want to say,
37
37
00:01:56,087 --> 00:02:00,396
well I'm gonna basically say the opposite
to what's happening up in here.
38
38
00:02:00,396 --> 00:02:05,836
When a block is destroyed we want
it to say hey, decrease the number
39
39
00:02:05,836 --> 00:02:12,653
of breakable blocks by saying breakable
blocks minus minus, and then semicolon.
40
40
00:02:12,653 --> 00:02:13,872
Now what else do we need to do?
41
41
00:02:13,872 --> 00:02:18,554
Well, within here as well, at the same
time that a block is destroyed,
42
42
00:02:18,554 --> 00:02:23,096
we want to say right now hey,
have the number of blocks reached zero?
43
43
00:02:23,096 --> 00:02:27,969
So put in an if statement, if,
and then our parenthesis we
44
44
00:02:27,969 --> 00:02:32,346
wanna say if the (breakableBlocks)
is less than or
45
45
00:02:32,346 --> 00:02:36,435
equal to 0,
then what do we want to have happen?
46
46
00:02:36,435 --> 00:02:41,090
Well, what we wanna have happen
is to load the next level.
47
47
00:02:41,090 --> 00:02:46,254
So we know from our ScreenLoader,
so we have a ScreenLoader class.
48
48
00:02:46,254 --> 00:02:50,752
We've already created a load next
level method, so we wanna call that.
49
49
00:02:50,752 --> 00:02:54,651
If you remember,
I'll find object of thought.
50
50
00:02:54,651 --> 00:02:57,599
And once again,
I will cache this as a reference just so
51
51
00:02:57,599 --> 00:03:01,075
that we don't have to find the object
of type every single time.
52
52
00:03:01,075 --> 00:03:05,415
And so to do that we will say SceneLoader,
53
53
00:03:05,415 --> 00:03:10,754
capital S, SceneLoader,
and then sceneloader.
54
54
00:03:10,754 --> 00:03:13,526
So this is just a way of
saying we now have a variable,
55
55
00:03:13,526 --> 00:03:16,308
sceneloader variable that
have type SceneLoader.
56
56
00:03:16,308 --> 00:03:20,734
And again I'll just make a note in here,
cached reference, so
57
57
00:03:20,734 --> 00:03:24,491
that we know this is our cache
not the thought we have,
58
58
00:03:24,491 --> 00:03:28,849
our parameters, parameters,
that we need to keep track of.
59
59
00:03:28,849 --> 00:03:32,427
Just helping you guys see that this is
the structure we have at the top of our
60
60
00:03:32,427 --> 00:03:36,085
script, so you can get into the habit
of putting things in the right place.
61
61
00:03:36,085 --> 00:03:38,953
You don't have to write in
your comments each time,
62
62
00:03:38,953 --> 00:03:42,613
I'm just doing that to make it clear
from a teaching point of view.
63
63
00:03:42,613 --> 00:03:47,045
Okay, so we have our scene loader,
now if breakableBlocks is less than,
64
64
00:03:47,045 --> 00:03:48,912
sorry in our start method now.
65
65
00:03:48,912 --> 00:03:50,014
I've jumped a step ahead.
66
66
00:03:50,014 --> 00:03:51,004
What do we need to do next?
67
67
00:03:51,004 --> 00:03:51,773
This is a question for you.
68
68
00:03:51,773 --> 00:03:53,345
Many challenge for you.
69
69
00:03:53,345 --> 00:03:56,779
How do we get access to our sceneloader?
70
70
00:03:56,779 --> 00:03:59,469
How do we cache that?
71
71
00:03:59,469 --> 00:04:01,945
Well, we say Start.
72
72
00:04:01,945 --> 00:04:08,479
Hit Tab to autocomplete, and
then we will say sceneloader = what?
73
73
00:04:08,479 --> 00:04:12,520
Another mini challenge for
you, complete that sentence.
74
74
00:04:12,520 --> 00:04:16,538
sceneloader = FindObjectOfType.
75
75
00:04:16,538 --> 00:04:21,258
Angle brackets, and within here we're
finding capital s breaks Sceneloader.
76
76
00:04:21,258 --> 00:04:23,924
Open and close parenthesis, semicolon.
77
77
00:04:23,924 --> 00:04:28,422
So now we know that SceneLoader is
a cash reference to the class or
78
78
00:04:28,422 --> 00:04:30,854
the objective type SceneLoader.
79
79
00:04:30,854 --> 00:04:36,397
Now in our if statement down here, we can
say, cuz we need to access sceneloader.
80
80
00:04:36,397 --> 00:04:37,414
What are we accessing in there?
81
81
00:04:37,414 --> 00:04:39,460
We're accessing load next scene.
82
82
00:04:39,460 --> 00:04:42,330
So, here's another mini challenge for you.
83
83
00:04:42,330 --> 00:04:45,162
Let's load the next level.
84
84
00:04:45,162 --> 00:04:47,284
Okay, you're just waiting for
me to continue,
85
85
00:04:47,284 --> 00:04:49,373
are you actually doing
these mini challenges?
86
86
00:04:49,373 --> 00:04:52,846
It's okay if you're not but this is how
you really get better at these things.
87
87
00:04:52,846 --> 00:04:56,183
If you push yourself and pause and
try to figure it out on your own.
88
88
00:04:56,183 --> 00:04:58,386
That's how this really embeds.
89
89
00:04:58,386 --> 00:05:00,652
It's cool if you're not, but
it's even better if you are.
90
90
00:05:00,652 --> 00:05:07,858
So we'll say in here
sceneloader.LoadNextScene.
91
91
00:05:07,858 --> 00:05:10,065
Open and closed brackets and semi-colon.
92
92
00:05:10,065 --> 00:05:14,583
So now we've created our, when a block
is destroyed, we want to yell hey,
93
93
00:05:14,583 --> 00:05:15,890
block is destroyed.
94
94
00:05:15,890 --> 00:05:19,501
We decrease the number of blocks
by one that are breakable, and
95
95
00:05:19,501 --> 00:05:22,852
then we say if it's reached 0,
then load the next scene.
96
96
00:05:22,852 --> 00:05:25,360
I'm gonna save that hop over into block,
and
97
97
00:05:25,360 --> 00:05:28,676
this is where I'll give you
your proper full on challenge.
98
98
00:05:28,676 --> 00:05:30,095
That was our last level.
99
99
00:05:30,095 --> 00:05:32,042
It's our last lecturer.
100
100
00:05:32,042 --> 00:05:32,745
That's what I'm after.
101
101
00:05:32,745 --> 00:05:34,086
Update Block.cs.
102
102
00:05:34,086 --> 00:05:35,204
So here is the challenge.
103
103
00:05:35,204 --> 00:05:40,504
Call our method for decreasing the number
of blocks when a block is destroyed.
104
104
00:05:40,504 --> 00:05:44,693
Update Block.cs, and ensure that you
have your build settings correct.
105
105
00:05:44,693 --> 00:05:46,736
We haven't done that yet for this scene.
106
106
00:05:46,736 --> 00:05:48,610
You might have done it but
I haven't done it yet.
107
107
00:05:48,610 --> 00:05:50,956
And to test and rejoice that it works.
108
108
00:05:50,956 --> 00:05:52,336
And another little hint for
109
109
00:05:52,336 --> 00:05:56,416
you, you might wanna decrease the number
of blocks in your scene to one just so you
110
110
00:05:56,416 --> 00:06:00,565
don't have to get rid of all of the blocks
you can text it a little bit more quickly.
111
111
00:06:00,565 --> 00:06:04,047
Okay, so as your challenge jump in and
get started.
112
112
00:06:04,047 --> 00:06:07,702
Okay, so within Block.cs,
we need to find our OnCollisionEnter,
113
113
00:06:07,702 --> 00:06:10,103
this is where things
are getting destroyed.
114
114
00:06:10,103 --> 00:06:13,902
Underneath Destroy, we want to
give an update in here that says,
115
115
00:06:13,902 --> 00:06:17,234
well not an update, but
we wanna call our method in here.
116
116
00:06:17,234 --> 00:06:21,327
I'm thinking that it's updating the
situation but it's not the update method.
117
117
00:06:21,327 --> 00:06:26,990
We're saying level, we've already created
ourselves a cash reference to the level.
118
118
00:06:26,990 --> 00:06:30,892
So level., and what do we have in here?
119
119
00:06:30,892 --> 00:06:37,029
It's gonna be BlockDestroyed );.
120
120
00:06:37,029 --> 00:06:42,311
So we're saying access the level class
that we're doing and lock is destroyed.
121
121
00:06:42,311 --> 00:06:43,581
Pretty straightforward.
122
122
00:06:43,581 --> 00:06:46,716
Now I'm gonna do one more step while I'm
in here which is just to factor this out
123
123
00:06:46,716 --> 00:06:47,344
to clean it up.
124
124
00:06:47,344 --> 00:06:49,006
This is a refactor, it's not critical.
125
125
00:06:49,006 --> 00:06:51,965
Things will still work without it,
but this is just for readability.
126
126
00:06:51,965 --> 00:06:55,143
Not to do too many things
under this OnCollisionEnter.
127
127
00:06:55,143 --> 00:06:58,561
So I'm gonna highlight my
three lines here, right-click.
128
128
00:06:58,561 --> 00:07:00,085
Slightly different on the Mac, you'll see.
129
129
00:07:00,085 --> 00:07:02,525
Quick Fix and then Extract Method.
130
130
00:07:02,525 --> 00:07:07,893
On a PC, Quick Actions and
Refactorings, and then Extract Method.
131
131
00:07:07,893 --> 00:07:14,740
We'll call this method DestroyBlock,
and then hit Enter.
132
132
00:07:14,740 --> 00:07:23,132
So basically, Whenever we collide with the
block, then we want to destroy the block.
133
133
00:07:23,132 --> 00:07:26,295
Okay, I'll call DestroyBlock or,
yeah, that's good for now.
134
134
00:07:26,295 --> 00:07:29,389
It'll change a little bit later on, but
for now, this is what we've got, so
135
135
00:07:29,389 --> 00:07:30,729
this is the best thing to call it.
136
136
00:07:30,729 --> 00:07:33,227
Save that, jump over into our game.
137
137
00:07:33,227 --> 00:07:36,614
Now, I mentioned about Build Settings,
Edit.
138
138
00:07:36,614 --> 00:07:40,678
Sorry, File > Build Settings.
139
139
00:07:40,678 --> 00:07:44,388
And one thing you'll notice with our Build
Settings is that we don't have any level
140
140
00:07:44,388 --> 00:07:45,289
two sitting there.
141
141
00:07:45,289 --> 00:07:49,166
So I'll just click Add Open Scenes,
cuz level two is open at the moment.
142
142
00:07:49,166 --> 00:07:53,542
Now I'm gonna cheat a little bit and drag
this above level one so that when we win
143
143
00:07:53,542 --> 00:07:58,076
level two it'll take us to level one,
just to make it easy for testing purposes.
144
144
00:07:58,076 --> 00:08:02,667
And as I mentioned before I'm gonna
select all of my blocks except for one.
145
145
00:08:02,667 --> 00:08:07,014
Delete those, click Play,
we should see if I click on level,
146
146
00:08:07,014 --> 00:08:11,698
we should see that there are one
breakable blocks, yes there are,
147
147
00:08:11,698 --> 00:08:16,490
click back in my game window,
[SOUND] and then plonk, and Game Over.
148
148
00:08:16,490 --> 00:08:19,227
Didn't work, okay,
what have we not done in here?
149
149
00:08:19,227 --> 00:08:25,386
We're looking for in our level, we didn't
go through this quite in enough detail.
150
150
00:08:25,386 --> 00:08:30,120
We have SceneLoader, I've forgotten that
we don't have SceneLoader in our scene at
151
151
00:08:30,120 --> 00:08:31,247
the moment, do we?
152
152
00:08:31,247 --> 00:08:35,801
That was an assumption that I made was
incorrect, so on level I'm going to drag
153
153
00:08:35,801 --> 00:08:39,674
in SceneLoader, so drag SceneLoader
as a component on level now.
154
154
00:08:39,674 --> 00:08:44,143
When we're saying fine object of type,
here it is right here sceneloaded.cs
155
155
00:08:44,143 --> 00:08:47,257
is another way of saying
it's the SceneLoader class,
156
156
00:08:47,257 --> 00:08:51,203
and it's in our scene because we
have it sitting right here on level.
157
157
00:08:51,203 --> 00:08:55,153
Make sure on level we can click Apply, so
it applies all these back to the prefab,
158
158
00:08:55,153 --> 00:08:58,600
so when we drop in level and
future levels, it knows what's going on.
159
159
00:08:58,600 --> 00:09:01,643
So just a reminder,
why I was doing that is we're looking for
160
160
00:09:01,643 --> 00:09:03,556
FindObjectOfType SceneLoader, but
161
161
00:09:03,556 --> 00:09:07,573
we didn't actually have any SceneLoaders
in our scene just yet, and now we do.
162
162
00:09:07,573 --> 00:09:12,399
So save everything up, click on Play,
look to see if there's any errors.
163
163
00:09:12,399 --> 00:09:14,808
I don't know how long that errors
been there but it's now gone.
164
164
00:09:14,808 --> 00:09:16,856
So there is no error, fantastic.
165
165
00:09:16,856 --> 00:09:20,557
And we click and it should,
plonk, load into the other scene.
166
166
00:09:20,557 --> 00:09:23,367
That's great, very excited to see that.
167
167
00:09:23,367 --> 00:09:27,071
So excellent work,
we now have a fully functioning game.
168
168
00:09:27,071 --> 00:09:29,545
You can add a couple of these
blocks back if you want.
169
169
00:09:29,545 --> 00:09:32,162
I'm just gonna add one more,
so we've got two sets.
170
170
00:09:32,162 --> 00:09:33,964
It looks a little bit more like a game.
171
171
00:09:33,964 --> 00:09:37,468
And so when we complete this scene,
it will load the next scene.
172
172
00:09:37,468 --> 00:09:39,898
That area has reappeared down
the bottom of the screen but
173
173
00:09:39,898 --> 00:09:41,578
that should go away when we click Play.
174
174
00:09:41,578 --> 00:09:45,858
We can now play fully through a full
game loop where the player can start,
175
175
00:09:45,858 --> 00:09:47,974
win, lose, and restart the game.
176
176
00:09:47,974 --> 00:09:50,350
So great work, and
I'll see you in the next lecture.
17873
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.