00:00:00,000 --> 00:00:02,450
[Music]

00:00:02,600 --> 00:00:06,299
hello everyone welcome to this video on

00:00:04,680 --> 00:00:08,130
making a tower defense game in unity

00:00:06,299 --> 00:00:10,679
today we are going to have a look at

00:00:08,130 --> 00:00:12,420
unlocking levels but Before we jump into

00:00:10,679 --> 00:00:14,009
unity and get started I just have a few

00:00:12,420 --> 00:00:16,170
things that I wanted to address the

00:00:14,009 --> 00:00:18,449
first one being I am now on a fixed

00:00:16,170 --> 00:00:20,550
video schedule that means every

00:00:18,449 --> 00:00:22,800
Wednesday and Sunday that's Wednesday

00:00:20,550 --> 00:00:24,630
and Sunday I'll be uploading a new

00:00:22,800 --> 00:00:26,699
tutorial I've actually been on the

00:00:24,630 --> 00:00:29,519
schedule for quite a while but for some

00:00:26,699 --> 00:00:32,340
reason I just haven't really told it to

00:00:29,519 --> 00:00:34,200
you in a video and yeah no particular

00:00:32,340 --> 00:00:35,790
reason why I'm just a doofus and the

00:00:34,200 --> 00:00:39,210
second thing is I got a new camera and

00:00:35,790 --> 00:00:40,800
I've been experimenting a bit with kind

00:00:39,210 --> 00:00:42,750
of the layout and setup for these

00:00:40,800 --> 00:00:46,530
tutorials so let me know what you think

00:00:42,750 --> 00:00:48,000
of this new setup and yeah please be

00:00:46,530 --> 00:00:50,610
constructive about it but I definitely

00:00:48,000 --> 00:00:52,379
want to hear what you guys have to say

00:00:50,610 --> 00:00:53,730
so that I can fine-tune it because I'm

00:00:52,379 --> 00:00:55,530
still figuring out some of this stuff

00:00:53,730 --> 00:00:58,079
and that's also the reason why this

00:00:55,530 --> 00:01:00,480
particular video is uploaded on Thursday

00:00:58,079 --> 00:01:02,070
and not a Wednesday is because I messed

00:01:00,480 --> 00:01:04,409
up something with the new camera it's

00:01:02,070 --> 00:01:07,229
pretty embarrassing but I shouldn't have

00:01:04,409 --> 00:01:08,790
nice stuff so yeah I had to postpone it

00:01:07,229 --> 00:01:10,979
a single day oh and I forgot to mention

00:01:08,790 --> 00:01:12,600
that this previous weekend was Ludum

00:01:10,979 --> 00:01:14,490
dare a which is an awesome game

00:01:12,600 --> 00:01:16,920
development competition where you get 48

00:01:14,490 --> 00:01:18,840
hours to make a game from scratch using

00:01:16,920 --> 00:01:21,540
only code and assets that you create

00:01:18,840 --> 00:01:23,580
yourself I participated and you can find

00:01:21,540 --> 00:01:25,619
my submission in the link in the

00:01:23,580 --> 00:01:27,330
description so check that out if you

00:01:25,619 --> 00:01:29,640
want and definitely expect a

00:01:27,330 --> 00:01:31,920
behind-the-scenes video as soon as I can

00:01:29,640 --> 00:01:34,020
get that done so yeah let's jump into

00:01:31,920 --> 00:01:36,780
unity so let's get started by opening up

00:01:34,020 --> 00:01:38,759
our level select scene and also our

00:01:36,780 --> 00:01:40,560
level selector script so just double

00:01:38,759 --> 00:01:42,899
click that and it will boot up in Visual

00:01:40,560 --> 00:01:44,670
Studio and here you can see we simply

00:01:42,899 --> 00:01:47,159
have a reference to our fredo script

00:01:44,670 --> 00:01:50,100
which is responsible for changing scenes

00:01:47,159 --> 00:01:53,159
smoothly and we also have our public

00:01:50,100 --> 00:01:55,350
void select and that's currently just

00:01:53,159 --> 00:01:57,840
called by the different buttons but our

00:01:55,350 --> 00:01:59,700
script has no reference to the buttons

00:01:57,840 --> 00:02:02,250
in our scene it doesn't know what they

00:01:59,700 --> 00:02:05,189
are they simply call this function so

00:02:02,250 --> 00:02:07,500
let's start by creating a list or more

00:02:05,189 --> 00:02:09,720
specifically an array of all of the

00:02:07,500 --> 00:02:11,819
buttons that we currently use in order

00:02:09,720 --> 00:02:13,920
to be able to change to a certain level

00:02:11,819 --> 00:02:17,160
so to do that let's create

00:02:13,920 --> 00:02:19,290
of course a public variable and we need

00:02:17,160 --> 00:02:21,540
these to be of type button but that's

00:02:19,290 --> 00:02:25,920
not included by default we need to go up

00:02:21,540 --> 00:02:28,200
here and say using unity engine dot UI

00:02:25,920 --> 00:02:30,959
and now we have access to the button

00:02:28,200 --> 00:02:33,230
class then we can call the button array

00:02:30,959 --> 00:02:35,970
here and we need to mark it as an array

00:02:33,230 --> 00:02:38,430
by opening and closing these square

00:02:35,970 --> 00:02:42,360
parentheses we can call this one our

00:02:38,430 --> 00:02:45,569
level buttons now when we save this and

00:02:42,360 --> 00:02:48,000
head back into unity and when we then

00:02:45,569 --> 00:02:49,980
select our level selector object with

00:02:48,000 --> 00:02:52,650
our script we should see a new array

00:02:49,980 --> 00:02:55,290
that we can resize more specifically we

00:02:52,650 --> 00:02:57,060
can add two new elements and of course

00:02:55,290 --> 00:02:59,070
we want to add our two buttons here so

00:02:57,060 --> 00:03:01,260
let's just find that those under the

00:02:59,070 --> 00:03:03,269
scroll rect under the content and we

00:03:01,260 --> 00:03:05,130
have our first level button and our

00:03:03,269 --> 00:03:08,160
second level button and we'll just drag

00:03:05,130 --> 00:03:11,160
those in so what this allows us to do is

00:03:08,160 --> 00:03:13,530
now in our script at any point let's

00:03:11,160 --> 00:03:17,100
just choose the start method here so

00:03:13,530 --> 00:03:18,989
right when we boot up this scene what we

00:03:17,100 --> 00:03:21,049
want to do is loop through all of our

00:03:18,989 --> 00:03:23,489
buttons so we want to go for and

00:03:21,049 --> 00:03:25,590
whenever you create a for statement you

00:03:23,489 --> 00:03:29,430
could go ahead and type it out yourself

00:03:25,590 --> 00:03:33,239
so far in I equals 0 we want to go as

00:03:29,430 --> 00:03:36,600
long as I is less than level buttons dot

00:03:33,239 --> 00:03:38,130
length but this is always a bit

00:03:36,600 --> 00:03:40,890
redundant what we'll do instead is just

00:03:38,130 --> 00:03:42,840
write for and then tab tab and that's

00:03:40,890 --> 00:03:44,160
going to automatically insert all of

00:03:42,840 --> 00:03:46,260
this stuff forth so we can name our

00:03:44,160 --> 00:03:48,690
variable we could call it J for example

00:03:46,260 --> 00:03:50,940
but we'll just call it I and we'll also

00:03:48,690 --> 00:03:54,000
change the length here to level buttons

00:03:50,940 --> 00:03:57,600
length and this way we quickly have a

00:03:54,000 --> 00:04:00,840
for loop that will go through and for

00:03:57,600 --> 00:04:02,940
each button in our array up here we'll

00:04:00,840 --> 00:04:04,530
create a variable I with the index of

00:04:02,940 --> 00:04:07,200
that button so we'll just keep going

00:04:04,530 --> 00:04:08,610
until we reach the end of our array and

00:04:07,200 --> 00:04:12,000
what this allows us to do is now say

00:04:08,610 --> 00:04:13,560
level buttons to the ith element so

00:04:12,000 --> 00:04:15,840
which is the first time we go through

00:04:13,560 --> 00:04:19,049
it's going to select the first button

00:04:15,840 --> 00:04:20,640
which has an index of 0 the second time

00:04:19,049 --> 00:04:23,880
it's going to select the second button

00:04:20,640 --> 00:04:25,320
which has an index of 1 and of course

00:04:23,880 --> 00:04:26,940
it's going to stop there for us but you

00:04:25,320 --> 00:04:29,100
can have an unlimited or

00:04:26,940 --> 00:04:31,020
at least a pretty long list of buttons

00:04:29,100 --> 00:04:33,140
on here and it's not going to complain

00:04:31,020 --> 00:04:35,850
about it and then we can go dot

00:04:33,140 --> 00:04:38,190
interactable and we can then disable the

00:04:35,850 --> 00:04:40,770
interact ability of these buttons so

00:04:38,190 --> 00:04:42,990
this really does through script is go to

00:04:40,770 --> 00:04:45,090
our button and then disables the

00:04:42,990 --> 00:04:48,240
interact ability here so it just toggles

00:04:45,090 --> 00:04:50,100
off this switch right here and of course

00:04:48,240 --> 00:04:52,200
we do that for both buttons right now so

00:04:50,100 --> 00:04:54,300
if we go and hit play we should see that

00:04:52,200 --> 00:04:56,130
we don't see any hover animation and

00:04:54,300 --> 00:04:59,010
that we cannot actually click these

00:04:56,130 --> 00:05:01,650
buttons what a standard button will do

00:04:59,010 --> 00:05:03,810
just a default one is because it uses

00:05:01,650 --> 00:05:06,570
the colored tint as a transition we'll

00:05:03,810 --> 00:05:08,730
go ahead and maybe fade out the button a

00:05:06,570 --> 00:05:11,010
little bit so it's clear that you cannot

00:05:08,730 --> 00:05:12,720
interact with it but we don't do that at

00:05:11,010 --> 00:05:15,090
the moment so let's instead go in and

00:05:12,720 --> 00:05:16,890
create a small animation just like we

00:05:15,090 --> 00:05:19,230
have the normal highlighted and pressed

00:05:16,890 --> 00:05:21,900
we also want to go and do something for

00:05:19,230 --> 00:05:24,660
the disabled and what we want to do with

00:05:21,900 --> 00:05:27,960
this animation is simply find our color

00:05:24,660 --> 00:05:30,060
and then drag down the Alpha quite a bit

00:05:27,960 --> 00:05:32,870
just to make it very very clear that

00:05:30,060 --> 00:05:35,370
this button is not to be interacted with

00:05:32,870 --> 00:05:37,560
but remember whenever we change a

00:05:35,370 --> 00:05:39,660
parameter on one of the animations we

00:05:37,560 --> 00:05:42,210
have to make sure that it goes back if

00:05:39,660 --> 00:05:44,490
we then re-enable it we don't want it to

00:05:42,210 --> 00:05:46,950
just change its alpha and if it then

00:05:44,490 --> 00:05:48,810
goes back to normal it will stay at that

00:05:46,950 --> 00:05:51,210
alpha and we of course have to input a

00:05:48,810 --> 00:05:54,840
keyframe here for the color as well

00:05:51,210 --> 00:05:56,880
where the Alpha is 255 so we'll just go

00:05:54,840 --> 00:05:59,160
ahead and duplicate this keyframe here

00:05:56,880 --> 00:06:02,010
and paste that on the highlighted as

00:05:59,160 --> 00:06:04,350
well and undepressed so it's only when

00:06:02,010 --> 00:06:08,160
it's disabled that we will get this on

00:06:04,350 --> 00:06:11,040
faded color and we can do this on one of

00:06:08,160 --> 00:06:13,230
the objects only and we don't even even

00:06:11,040 --> 00:06:15,000
have to do any applying of the prefab

00:06:13,230 --> 00:06:16,980
because they all share the same

00:06:15,000 --> 00:06:19,260
animation files and that's what we've

00:06:16,980 --> 00:06:22,440
just edited we just edited right now

00:06:19,260 --> 00:06:26,640
this button here that has the disabled

00:06:22,440 --> 00:06:29,100
highlighted normal and pressed cool so

00:06:26,640 --> 00:06:32,190
what we should now see is that when we

00:06:29,100 --> 00:06:34,830
hit play they're both faded out and

00:06:32,190 --> 00:06:37,620
that's awesome one thing that we of

00:06:34,830 --> 00:06:39,930
course do not want to happen is that

00:06:37,620 --> 00:06:40,680
right now both of these buttons are

00:06:39,930 --> 00:06:43,080
faded out

00:06:40,680 --> 00:06:46,080
and we want to always be able to choose

00:06:43,080 --> 00:06:48,210
at least one so of course we have to

00:06:46,080 --> 00:06:51,449
keep track of what level we're currently

00:06:48,210 --> 00:06:54,600
reached and in order to do this um it's

00:06:51,449 --> 00:06:57,000
not enough to just ah create a variable

00:06:54,600 --> 00:06:59,669
because a variable won't persist between

00:06:57,000 --> 00:07:01,440
scenes if we create the same script in

00:06:59,669 --> 00:07:04,050
the first scene and in the second scene

00:07:01,440 --> 00:07:06,780
it's not going to carry over if we mark

00:07:04,050 --> 00:07:08,130
it as a public static variable that is

00:07:06,780 --> 00:07:10,020
totally going to work and it's going to

00:07:08,130 --> 00:07:12,210
carry over but as soon as you close down

00:07:10,020 --> 00:07:14,760
the game your progress is going to be

00:07:12,210 --> 00:07:17,039
lost and the static variable is just

00:07:14,760 --> 00:07:20,220
going to go back to its default value so

00:07:17,039 --> 00:07:24,650
what we do instead is save our progress

00:07:20,220 --> 00:07:28,050
to some kind of file many advanced games

00:07:24,650 --> 00:07:31,830
use some kind of data format for this

00:07:28,050 --> 00:07:34,590
and that could be JSON or XML which are

00:07:31,830 --> 00:07:36,210
basically just formats for storing loads

00:07:34,590 --> 00:07:39,449
of data say you want to store

00:07:36,210 --> 00:07:42,150
playerhealth player progress levels all

00:07:39,449 --> 00:07:43,949
that stuff maybe even where objects are

00:07:42,150 --> 00:07:46,470
placed in the scene where your player is

00:07:43,949 --> 00:07:48,270
in the world when he locks out stuff

00:07:46,470 --> 00:07:50,460
like that you can store a lot of stuff

00:07:48,270 --> 00:07:51,300
in those formats and you can definitely

00:07:50,460 --> 00:07:55,139
check that out

00:07:51,300 --> 00:07:57,479
it's called serialization but what we do

00:07:55,139 --> 00:08:00,210
which is a lot easier and it's something

00:07:57,479 --> 00:08:03,330
that unity has provided us for this use

00:08:00,210 --> 00:08:06,210
case specifically is use the built-in

00:08:03,330 --> 00:08:08,460
player preps and this just allows us to

00:08:06,210 --> 00:08:10,229
store either is string a float or an

00:08:08,460 --> 00:08:12,389
integer and you can store multiple

00:08:10,229 --> 00:08:14,669
integers multiple floats multiple

00:08:12,389 --> 00:08:17,930
strings so you can actually use it for a

00:08:14,669 --> 00:08:20,370
lot of stuff really really easily so

00:08:17,930 --> 00:08:22,710
what we do in order to utilize this

00:08:20,370 --> 00:08:26,010
function is let's create a new variable

00:08:22,710 --> 00:08:28,050
and let's call it level reached this is

00:08:26,010 --> 00:08:30,659
going to be the level of the highest

00:08:28,050 --> 00:08:33,209
number that we have reached so in the

00:08:30,659 --> 00:08:35,669
beginning this is going to say 1 then

00:08:33,209 --> 00:08:38,250
when we beat that level we get to go to

00:08:35,669 --> 00:08:41,339
level 2 so we level reached will be 2

00:08:38,250 --> 00:08:44,279
and so on so we don't want to set this

00:08:41,339 --> 00:08:46,290
equal to either 1 or 2 instead we want

00:08:44,279 --> 00:08:48,390
to set it equal to the level that we

00:08:46,290 --> 00:08:51,570
have reached and therefore we go to

00:08:48,390 --> 00:08:53,680
player prefs and you use this whenever

00:08:51,570 --> 00:08:56,770
you want to get a value or

00:08:53,680 --> 00:08:59,589
setup value and then we do dot and in

00:08:56,770 --> 00:09:01,779
this case we want to get a value so we

00:08:59,589 --> 00:09:04,990
go get and then you can see we have both

00:09:01,779 --> 00:09:07,089
string in and float in an our case

00:09:04,990 --> 00:09:09,460
because it's a limited amount of levels

00:09:07,089 --> 00:09:12,670
we want to just use the integer some

00:09:09,460 --> 00:09:16,089
number some whole number so let's use

00:09:12,670 --> 00:09:18,850
get int and then we give it the name of

00:09:16,089 --> 00:09:21,610
the key and the key is basically just a

00:09:18,850 --> 00:09:24,339
string value that you used to associate

00:09:21,610 --> 00:09:27,310
with that number it's basically the same

00:09:24,339 --> 00:09:30,070
thing as a variable name so if we treat

00:09:27,310 --> 00:09:32,410
this integer as a variable that has a

00:09:30,070 --> 00:09:35,830
value and the key to get that value is

00:09:32,410 --> 00:09:38,459
the name this uses the exact same thing

00:09:35,830 --> 00:09:40,839
so our key here could be the exact same

00:09:38,459 --> 00:09:43,600
level reached and I think we should just

00:09:40,839 --> 00:09:46,570
do that for simplicity we also get to

00:09:43,600 --> 00:09:49,450
specify a default number so in our case

00:09:46,570 --> 00:09:52,870
we do comma one and that's because we

00:09:49,450 --> 00:09:55,209
want our default level reached to be one

00:09:52,870 --> 00:09:57,100
we do this so that if we have never

00:09:55,209 --> 00:10:00,820
played the game before and it tries to

00:09:57,100 --> 00:10:02,500
go here and find a saved piece of data

00:10:00,820 --> 00:10:04,810
called level reached and it isn't

00:10:02,500 --> 00:10:07,390
created yet it's just going to return

00:10:04,810 --> 00:10:09,490
the value one and so it's only going to

00:10:07,390 --> 00:10:11,200
allow us to play the first level this is

00:10:09,490 --> 00:10:14,320
going to happen every time a new player

00:10:11,200 --> 00:10:16,900
plays the game so now we can use this

00:10:14,320 --> 00:10:19,570
level reached in order to specify what

00:10:16,900 --> 00:10:21,760
level buttons we want to actually be

00:10:19,570 --> 00:10:26,980
interactable and we do that by saying

00:10:21,760 --> 00:10:30,730
that if the current value of our I here

00:10:26,980 --> 00:10:34,560
so if I which means the index of the

00:10:30,730 --> 00:10:38,410
button we are currently on is less than

00:10:34,560 --> 00:10:41,440
level reached well then we want to go

00:10:38,410 --> 00:10:43,779
ahead and make it not interactable so if

00:10:41,440 --> 00:10:46,209
we haven't reached that level yet if

00:10:43,779 --> 00:10:48,160
this current button has a level index

00:10:46,209 --> 00:10:51,100
that we haven't reached we want to go

00:10:48,160 --> 00:10:53,920
ahead and make it non interactable but

00:10:51,100 --> 00:10:56,470
of course our eye here starts zero and

00:10:53,920 --> 00:10:59,410
obviously our level reached starts at

00:10:56,470 --> 00:11:01,510
one so we have to just add one on to the

00:10:59,410 --> 00:11:03,520
eye in order to make that totally valid

00:11:01,510 --> 00:11:05,620
whoops I noticed a bug here that we have

00:11:03,520 --> 00:11:07,600
to fix before we run this and that is if

00:11:05,620 --> 00:11:09,670
I plus one is grew

00:11:07,600 --> 00:11:12,399
rather than the level that we reached

00:11:09,670 --> 00:11:14,649
then we want to disable it awesome so we

00:11:12,399 --> 00:11:16,750
can save that hidden to unity and what

00:11:14,649 --> 00:11:18,730
we should see now is that when we play

00:11:16,750 --> 00:11:22,149
the game it's not going to have any

00:11:18,730 --> 00:11:24,339
previously stored data and so it's just

00:11:22,149 --> 00:11:25,990
going to return one and that means that

00:11:24,339 --> 00:11:27,610
the only button that's going to be

00:11:25,990 --> 00:11:29,949
clickable is the first one and the

00:11:27,610 --> 00:11:32,290
second one should be grayed out so if we

00:11:29,949 --> 00:11:34,750
play now we should see exactly that

00:11:32,290 --> 00:11:37,180
happening and that's awesome and again

00:11:34,750 --> 00:11:40,060
we cannot click this but we can proceed

00:11:37,180 --> 00:11:42,339
to the first level so that's actually

00:11:40,060 --> 00:11:44,319
all we need to do for a level selector

00:11:42,339 --> 00:11:46,240
but of course we still have to change

00:11:44,319 --> 00:11:48,670
our level reach so that we can actually

00:11:46,240 --> 00:11:51,250
progress in the game it's not going to

00:11:48,670 --> 00:11:54,850
do that automatically so what we do here

00:11:51,250 --> 00:11:56,980
is head into our first level we find our

00:11:54,850 --> 00:11:59,019
game master so let's find the place in

00:11:56,980 --> 00:12:00,790
our code where our level ends and that's

00:11:59,019 --> 00:12:03,100
inside of our wave spawner so let's

00:12:00,790 --> 00:12:05,860
double click that and scroll down here

00:12:03,100 --> 00:12:08,110
to where inside of our spawn wave we

00:12:05,860 --> 00:12:10,209
realize that we've just spawned all of

00:12:08,110 --> 00:12:12,220
the waves that we wanted to have in this

00:12:10,209 --> 00:12:14,139
level we currently just throw out a

00:12:12,220 --> 00:12:16,389
debug deadlock statement saying that we

00:12:14,139 --> 00:12:18,939
won the level and then we disabled this

00:12:16,389 --> 00:12:20,680
script what we want to do instead is we

00:12:18,939 --> 00:12:22,750
probably still want to throw out a debug

00:12:20,680 --> 00:12:25,180
block and you might even display some

00:12:22,750 --> 00:12:26,889
cool information here like the amount of

00:12:25,180 --> 00:12:29,259
waves that you just went through and the

00:12:26,889 --> 00:12:32,829
amount of enemies you killed cash earned

00:12:29,259 --> 00:12:36,939
stuff like that but what we will also do

00:12:32,829 --> 00:12:39,430
is of course increased the level reached

00:12:36,939 --> 00:12:40,779
so in order to do all of this stuff I

00:12:39,430 --> 00:12:42,939
don't want to do it inside the wave

00:12:40,779 --> 00:12:45,370
spawner I want a more appropriate place

00:12:42,939 --> 00:12:47,889
to put this code and that is of course

00:12:45,370 --> 00:12:50,529
inside of our game manager this is

00:12:47,889 --> 00:12:52,959
responsible for ending a game and so I

00:12:50,529 --> 00:12:55,540
think it should also be responsible for

00:12:52,959 --> 00:12:58,000
winning a level so that's going to and

00:12:55,540 --> 00:13:00,459
create a public void and let's call this

00:12:58,000 --> 00:13:03,430
one something like level 1 or you could

00:13:00,459 --> 00:13:06,399
just put win level which I think we are

00:13:03,430 --> 00:13:08,560
going to put and enough in here we could

00:13:06,399 --> 00:13:10,060
throw stuff like a debug deadlock

00:13:08,560 --> 00:13:11,829
statement and I think we're just going

00:13:10,060 --> 00:13:14,740
to copy paste that in there so we know

00:13:11,829 --> 00:13:16,899
what is going on and of course we need

00:13:14,740 --> 00:13:19,059
now a reference to the game manager

00:13:16,899 --> 00:13:20,450
inside of a wave spawn in order to call

00:13:19,059 --> 00:13:21,950
that function

00:13:20,450 --> 00:13:26,570
let's go to the top here and let's

00:13:21,950 --> 00:13:28,370
create a public game manager and we're

00:13:26,570 --> 00:13:31,400
going to call it a game manager as well

00:13:28,370 --> 00:13:35,690
let's go down here and say game manager

00:13:31,400 --> 00:13:37,910
dot and then we call win level then

00:13:35,690 --> 00:13:40,070
inside of our win level script we threw

00:13:37,910 --> 00:13:42,200
out the debug deadlock we could then go

00:13:40,070 --> 00:13:45,230
in here and say player prefs

00:13:42,200 --> 00:13:48,740
dot and now we don't want to use get now

00:13:45,230 --> 00:13:51,860
we want to use set so we use set in and

00:13:48,740 --> 00:13:55,550
again we input a key which is the name

00:13:51,860 --> 00:13:58,040
of our value so our name is level

00:13:55,550 --> 00:14:00,590
reached and it's very important that you

00:13:58,040 --> 00:14:03,080
type this the exact same way that you

00:14:00,590 --> 00:14:05,480
did inside of the level selector it's

00:14:03,080 --> 00:14:08,030
not going to throw an error if you

00:14:05,480 --> 00:14:10,940
didn't so just go in here copy this

00:14:08,030 --> 00:14:12,680
paste it in here and you're good to go

00:14:10,940 --> 00:14:14,690
just make sure that that's correct

00:14:12,680 --> 00:14:17,210
because it's not going to complain about

00:14:14,690 --> 00:14:19,430
it if you make a mistake and then we

00:14:17,210 --> 00:14:22,340
throw a value and the value here could

00:14:19,430 --> 00:14:26,450
be in our case we want to transition to

00:14:22,340 --> 00:14:28,790
two if it's the first level and to three

00:14:26,450 --> 00:14:30,740
if it's the second and so this is

00:14:28,790 --> 00:14:33,230
something that will dynamically change

00:14:30,740 --> 00:14:35,960
one thing you could do is keep sort of

00:14:33,230 --> 00:14:38,630
an array a list of all of the different

00:14:35,960 --> 00:14:40,700
levels with an Associated index that you

00:14:38,630 --> 00:14:43,550
put in here that would be totally fine

00:14:40,700 --> 00:14:46,760
or you could at the top here inside the

00:14:43,550 --> 00:14:48,710
game manager have some variable let's

00:14:46,760 --> 00:14:50,750
start with a public string and this is

00:14:48,710 --> 00:14:54,400
going to be the name of the next level

00:14:50,750 --> 00:14:58,010
and we'll just default that to level two

00:14:54,400 --> 00:15:01,810
and we'll also have a public integer

00:14:58,010 --> 00:15:06,880
which is going to be the next level

00:15:01,810 --> 00:15:11,270
index or we could call it level to reach

00:15:06,880 --> 00:15:15,320
or let's say unlock and we can default

00:15:11,270 --> 00:15:17,900
that to two as well so this is a bit

00:15:15,320 --> 00:15:19,490
more fragile because you'll have to

00:15:17,900 --> 00:15:22,370
remember to write everything in there

00:15:19,490 --> 00:15:25,190
but you will have to add it to the level

00:15:22,370 --> 00:15:27,830
array anyways and it's just a lot easier

00:15:25,190 --> 00:15:30,020
so now we simply input level to unlock

00:15:27,830 --> 00:15:31,700
in here and then we want to actually

00:15:30,020 --> 00:15:33,750
change the scene

00:15:31,700 --> 00:15:36,630
we of course need another reference

00:15:33,750 --> 00:15:38,900
that to our fate script so we'll go up

00:15:36,630 --> 00:15:43,890
here and get again and we'll create a

00:15:38,900 --> 00:15:47,040
public screen or on scene fader and

00:15:43,890 --> 00:15:49,760
we'll call this one scene fader and now

00:15:47,040 --> 00:15:52,290
we can go down here and type scene fader

00:15:49,760 --> 00:15:56,580
f82 and the scene that we want to fade

00:15:52,290 --> 00:15:58,710
to is the next level awesome so that

00:15:56,580 --> 00:16:00,780
should actually work just fine so now we

00:15:58,710 --> 00:16:03,210
just need to go into our game master and

00:16:00,780 --> 00:16:06,150
we need to input this new information we

00:16:03,210 --> 00:16:08,220
need to drag the game monster or manager

00:16:06,150 --> 00:16:10,350
inside of the wave spawner scheme

00:16:08,220 --> 00:16:13,080
manager there we go so that's linked up

00:16:10,350 --> 00:16:15,360
we want to drag the scene fader into our

00:16:13,080 --> 00:16:18,360
game manager and we want to confirm that

00:16:15,360 --> 00:16:20,040
our next level is level 2 that's correct

00:16:18,360 --> 00:16:23,520
because we're currently on level 1 and

00:16:20,040 --> 00:16:25,920
the level 2 unlock is also to come

00:16:23,520 --> 00:16:29,250
that's also correct so now we can just

00:16:25,920 --> 00:16:31,830
save this and it should work right away

00:16:29,250 --> 00:16:35,100
so let's go to our level select let's

00:16:31,830 --> 00:16:37,380
hit play and we should now be able to go

00:16:35,100 --> 00:16:39,210
to the first level here and run through

00:16:37,380 --> 00:16:40,589
it but that's going to take some time so

00:16:39,210 --> 00:16:43,170
I thought we would just take a shortcut

00:16:40,589 --> 00:16:46,140
which means just loading up level 1 and

00:16:43,170 --> 00:16:50,280
finding our wave spawner and setting the

00:16:46,140 --> 00:16:52,260
time between wave to 0 just so it it

00:16:50,280 --> 00:16:56,160
will really really quickly run through

00:16:52,260 --> 00:16:58,410
here so let's just put in a lot of

00:16:56,160 --> 00:17:01,940
objects or game objects here to kill off

00:16:58,410 --> 00:17:04,079
the different waves as they spawn

00:17:01,940 --> 00:17:06,660
awesome so you can see how quickly that

00:17:04,079 --> 00:17:08,610
win and you also notice the bug but it

00:17:06,660 --> 00:17:10,350
transitioned to the second level here so

00:17:08,610 --> 00:17:12,780
let's focus on that and we'll fix the

00:17:10,350 --> 00:17:15,000
bug in a second so it turns second

00:17:12,780 --> 00:17:18,270
decision to the second level and now we

00:17:15,000 --> 00:17:20,760
can go and choose our level select again

00:17:18,270 --> 00:17:22,980
and we can just hit don't save on that

00:17:20,760 --> 00:17:25,860
we don't need to do that hit play and

00:17:22,980 --> 00:17:28,230
you can see that they are both unlocked

00:17:25,860 --> 00:17:30,540
so that's awesome and it's going to keep

00:17:28,230 --> 00:17:32,760
doing that if we create say if we just

00:17:30,540 --> 00:17:36,450
very quickly go in here and create

00:17:32,760 --> 00:17:39,450
another button so just duplicate level

00:17:36,450 --> 00:17:42,300
button too and we change the text here

00:17:39,450 --> 00:17:45,420
to 3 and we go into a level selector and

00:17:42,300 --> 00:17:47,100
add it to the array so we'll go 3 there

00:17:45,420 --> 00:17:49,170
add it in there and

00:17:47,100 --> 00:17:52,320
play you should see that that's not

00:17:49,170 --> 00:17:54,870
unlocked and indeed it is not so you can

00:17:52,320 --> 00:17:58,680
see how easy it is now to add new levels

00:17:54,870 --> 00:18:01,410
to this system so now let's just focus

00:17:58,680 --> 00:18:04,350
on our bug and we'll call that a day so

00:18:01,410 --> 00:18:07,560
let's go into our first level here and

00:18:04,350 --> 00:18:12,210
basically what happened is it marked on

00:18:07,560 --> 00:18:14,130
the level here as completed before we

00:18:12,210 --> 00:18:17,160
had actually destroyed all the enemies

00:18:14,130 --> 00:18:19,890
and it even went on to spawn a wave

00:18:17,160 --> 00:18:22,410
before a previous wave was completed and

00:18:19,890 --> 00:18:24,690
the reason why is because of the way

00:18:22,410 --> 00:18:28,770
that we are currently checking if a way

00:18:24,690 --> 00:18:31,560
wave is done and we do that by if we go

00:18:28,770 --> 00:18:33,990
into the wave spawning here each time we

00:18:31,560 --> 00:18:37,830
spawn an enemy we increase enemies alive

00:18:33,990 --> 00:18:40,170
by one and then when we kill an enemy if

00:18:37,830 --> 00:18:43,110
we find our enemies script so we'll go

00:18:40,170 --> 00:18:46,800
and double click on the enemy here every

00:18:43,110 --> 00:18:52,560
time we die we decrease enemies alive

00:18:46,800 --> 00:18:54,510
and if enemies alive reaches zero it's

00:18:52,560 --> 00:18:59,190
going to go ahead and spawn the next

00:18:54,510 --> 00:19:02,670
wave but the problem with this the

00:18:59,190 --> 00:19:05,490
problem with this is that we of course

00:19:02,670 --> 00:19:07,800
instantiate the enemies with a bit of

00:19:05,490 --> 00:19:09,630
delay in between them so we don't just

00:19:07,800 --> 00:19:11,850
go ahead and spawn all of the enemies at

00:19:09,630 --> 00:19:14,520
the same time you can see here that we

00:19:11,850 --> 00:19:16,830
wait the wave rate so one divided by the

00:19:14,520 --> 00:19:19,440
wait rate and therefore there's a bit of

00:19:16,830 --> 00:19:23,010
delay between each enemies spawn so if

00:19:19,440 --> 00:19:23,910
an enemy spawns and we destroy it before

00:19:23,010 --> 00:19:26,970
the next one

00:19:23,910 --> 00:19:29,160
the amount of enemies alive is going to

00:19:26,970 --> 00:19:31,980
be zero in the middle of the REA wave

00:19:29,160 --> 00:19:34,770
and if we're unlucky it's going to start

00:19:31,980 --> 00:19:37,770
the next one or transition to the new

00:19:34,770 --> 00:19:40,230
level so in order to fix this instead of

00:19:37,770 --> 00:19:43,230
increasing enemies alive when we are

00:19:40,230 --> 00:19:47,490
spawning enemies we're just going to set

00:19:43,230 --> 00:19:50,280
enemies alive equal to wave dot count

00:19:47,490 --> 00:19:51,870
before we actually start spawning so

00:19:50,280 --> 00:19:54,720
this way we'll just set the enemies

00:19:51,870 --> 00:19:58,260
alive to the amount of enemies that are

00:19:54,720 --> 00:20:00,470
going to be spawned and yeah just use

00:19:58,260 --> 00:20:02,659
that it's much simpler

00:20:00,470 --> 00:20:04,340
and it should work much better so if

00:20:02,659 --> 00:20:06,380
we're now go in here and set out time

00:20:04,340 --> 00:20:08,539
between wave to something like one let's

00:20:06,380 --> 00:20:11,240
just decrease it a little bit and we'll

00:20:08,539 --> 00:20:13,669
very quickly just run through this so

00:20:11,240 --> 00:20:15,200
let's create a bunch of turrets to take

00:20:13,669 --> 00:20:17,390
care of our enemies and we'll create

00:20:15,200 --> 00:20:19,970
some of higher level as well let's use

00:20:17,390 --> 00:20:22,460
the laser beam here you can see that the

00:20:19,970 --> 00:20:25,190
previous wave does not start until

00:20:22,460 --> 00:20:26,690
you've actually completed the or the

00:20:25,190 --> 00:20:28,730
next wave doesn't start until you've

00:20:26,690 --> 00:20:30,679
completed the previous one and once we

00:20:28,730 --> 00:20:33,049
get through the last wave it's going to

00:20:30,679 --> 00:20:37,220
transition so now that bug should be

00:20:33,049 --> 00:20:39,169
gone awesome so you will notice a slight

00:20:37,220 --> 00:20:41,510
disadvantage when it comes to just

00:20:39,169 --> 00:20:43,309
duplicating levels like this and that is

00:20:41,510 --> 00:20:46,130
all of the changes that we've made to

00:20:43,309 --> 00:20:49,400
our game master in here have not updated

00:20:46,130 --> 00:20:51,380
in level two you still have to set up

00:20:49,400 --> 00:20:53,809
the scene fader and game manager in

00:20:51,380 --> 00:20:55,549
level two what we can do since we don't

00:20:53,809 --> 00:20:57,650
have anything special going on in level

00:20:55,549 --> 00:20:59,900
two is just delete that and then

00:20:57,650 --> 00:21:02,480
reduplicate the level but of course

00:20:59,900 --> 00:21:05,240
that's not super awesome if you have a

00:21:02,480 --> 00:21:07,460
lot of stuff going on that is different

00:21:05,240 --> 00:21:09,350
between the two levels what I would then

00:21:07,460 --> 00:21:11,900
recommend is you can of course go ahead

00:21:09,350 --> 00:21:14,179
and make all of these different objects

00:21:11,900 --> 00:21:16,580
into prefabs that you just make sure to

00:21:14,179 --> 00:21:19,640
update whenever you change them and you

00:21:16,580 --> 00:21:22,070
can get really far using that method but

00:21:19,640 --> 00:21:23,870
if you are really serious about this and

00:21:22,070 --> 00:21:26,360
want to create a lot of different levels

00:21:23,870 --> 00:21:28,340
what I would recommend is you utilize

00:21:26,360 --> 00:21:31,250
the fact that unity has now created a

00:21:28,340 --> 00:21:33,080
way of loading different scenes in you

00:21:31,250 --> 00:21:35,539
create one scene for all of the stuff

00:21:33,080 --> 00:21:38,030
that is shared between all the scenes

00:21:35,539 --> 00:21:41,600
like the game master and the scene fader

00:21:38,030 --> 00:21:43,669
the node UI stuff like that that doesn't

00:21:41,600 --> 00:21:46,370
necessarily need to be interlinked with

00:21:43,669 --> 00:21:48,380
the actual layout of the level and then

00:21:46,370 --> 00:21:50,750
you have another scene which is odd

00:21:48,380 --> 00:21:53,659
which is different for each level which

00:21:50,750 --> 00:21:55,130
is the actual layout of the scene so

00:21:53,659 --> 00:21:56,840
that's something that you might take

00:21:55,130 --> 00:21:59,030
into consideration if you want me to

00:21:56,840 --> 00:22:01,070
show kind of how that's done of course

00:21:59,030 --> 00:22:04,760
let me know I think next up is probably

00:22:01,070 --> 00:22:07,130
some screen that will display in order

00:22:04,760 --> 00:22:08,480
to show how you did in the level I think

00:22:07,130 --> 00:22:10,820
that's pretty important instead of just

00:22:08,480 --> 00:22:12,530
going directly to the next one but that

00:22:10,820 --> 00:22:13,290
was pretty much all I wanted to show you

00:22:12,530 --> 00:22:15,420
in this

00:22:13,290 --> 00:22:17,220
video and I will see you in the next one

00:22:15,420 --> 00:22:19,230
thanks to have the awesome patreon

00:22:17,220 --> 00:22:21,060
supporters who donated in November and a

00:22:19,230 --> 00:22:23,250
special thank to sultan al sharif by

00:22:21,060 --> 00:22:24,930
samara fire james Calhoun and Robert

00:22:23,250 --> 00:22:28,430
Bonham become a patron yourself at

00:22:24,930 --> 00:22:28,430
patreon.com slash practice


