1
00:00:03,120 --> 00:00:03,390
Okay.

2
00:00:03,390 --> 00:00:06,750
I've got a bit of a problem and solution type lecture for us here.

3
00:00:06,750 --> 00:00:12,570
And along the way we're going to learn about public access modifier and the problem that we've got.

4
00:00:12,660 --> 00:00:15,720
I'm just going to put up my load level delay to one.

5
00:00:15,720 --> 00:00:19,890
So we've got a little bit longer when we bump our head, what happens is when we bump our head, I'll

6
00:00:19,890 --> 00:00:21,930
just spin around and bump.

7
00:00:22,470 --> 00:00:24,570
I can continue to move and wobble.

8
00:00:24,570 --> 00:00:27,630
I can hit the ground again, trigger a sound effect again.

9
00:00:27,630 --> 00:00:30,630
So I've got a few things in here that we want to stop and prevent.

10
00:00:30,630 --> 00:00:35,190
Basically, when the player crashes, I want to say you can't control your player anymore because you

11
00:00:35,190 --> 00:00:36,030
crashed.

12
00:00:36,030 --> 00:00:41,580
And I'm going to go about doing this in a way that allows us to understand public access modifiers.

13
00:00:41,580 --> 00:00:46,410
So looking at this, theoretically, we've got our player controller script, which is saying how we

14
00:00:46,410 --> 00:00:53,220
move our player, giving us the ability to rotate and speed up, etc., and we want to block that control.

15
00:00:53,220 --> 00:00:57,270
However, we've got our crash detector that says, Hey, I know when you crashed.

16
00:00:57,270 --> 00:01:02,190
So at the moment we've got a problem where these two scripts aren't speaking to each other and we're

17
00:01:02,190 --> 00:01:05,670
going to use the public keyword to resolve that.

18
00:01:05,670 --> 00:01:07,350
So let's jump into Visual Studio.

19
00:01:07,350 --> 00:01:09,660
I'm going to close a couple of things here.

20
00:01:09,660 --> 00:01:11,790
I don't need dust trail and I don't need finish line.

21
00:01:11,790 --> 00:01:14,640
We're just working in player, controller and crash detector.

22
00:01:14,640 --> 00:01:19,920
These are the two scripts and we've talked a little bit just very briefly about public versus private.

23
00:01:19,920 --> 00:01:24,300
You've noticed each time Private has appeared, I've got there and said, Oh, I don't need private,

24
00:01:24,300 --> 00:01:25,680
I'm going to delete that.

25
00:01:25,680 --> 00:01:26,790
So what's going on here?

26
00:01:26,790 --> 00:01:34,320
Well, by default, all of our variables and all of our methods are only available within this particular

27
00:01:34,320 --> 00:01:35,160
class.

28
00:01:35,160 --> 00:01:39,240
That is to say the scope of these things is local to this class.

29
00:01:39,240 --> 00:01:42,360
There's some terminology for use scope, in other words, where you can use it.

30
00:01:42,360 --> 00:01:43,980
And local to this class.

31
00:01:43,980 --> 00:01:49,650
We've also talked about how if we've got something within these curly braces, for example, if we were

32
00:01:49,650 --> 00:01:55,500
to go and define a variable within here, declare it and define it, then we can't use it outside of

33
00:01:55,500 --> 00:01:56,580
this scope.

34
00:01:56,580 --> 00:02:01,380
So those curly braces will define you can use something within here, but not elsewhere.

35
00:02:01,380 --> 00:02:05,790
And we've talked a little bit about member variables that we can set up at the top of our class and

36
00:02:05,790 --> 00:02:07,170
then use them anywhere.

37
00:02:07,500 --> 00:02:12,090
As a point of comparison, the scope of these things is throughout our entire class.

38
00:02:12,090 --> 00:02:17,070
However, how do we go about having something accessible from a different class?

39
00:02:17,070 --> 00:02:23,010
So for example, if I'm in crash detector at the moment and I want to have a method in here that in

40
00:02:23,010 --> 00:02:26,880
player controller we can call at the moment, we have no way of doing that.

41
00:02:26,880 --> 00:02:31,890
If I try to call something in here, for example, the reload scene that I've created, if I were to

42
00:02:31,890 --> 00:02:38,220
say call this particular reload scene method from player controller, we won't be allowed to do that

43
00:02:38,220 --> 00:02:40,800
because reload scene is private.

44
00:02:40,800 --> 00:02:42,150
Remember, it's private by default.

45
00:02:42,150 --> 00:02:47,730
Even if you don't see the keyword in there, we would need to use the keyword public in order for us

46
00:02:47,730 --> 00:02:51,810
to be able to call this method from other classes.

47
00:02:51,810 --> 00:02:53,190
And why is it set up like this?

48
00:02:53,190 --> 00:02:54,540
Well, it's safer that way.

49
00:02:54,540 --> 00:02:57,450
It means that we don't accidentally go and call things.

50
00:02:57,450 --> 00:02:59,400
It means that we are very explicit.

51
00:02:59,400 --> 00:03:05,040
We're going out of our way to say, I want this thing or whatever we set up to be able to be called

52
00:03:05,040 --> 00:03:06,270
from another place.

53
00:03:06,270 --> 00:03:08,520
The same is true for variables.

54
00:03:08,520 --> 00:03:15,960
So by default our variables are private, so private int whatever is our variable in here.

55
00:03:15,960 --> 00:03:23,070
So there's a variable we could make this public and then we could change update modify this variable

56
00:03:23,070 --> 00:03:25,770
from other classes or other scripts.

57
00:03:25,770 --> 00:03:31,110
So for example, we'll make this ten and then if we're over in, so this is in the crash detector,

58
00:03:31,110 --> 00:03:36,300
then over in player controller, we could access whatever and set whatever to 20.

59
00:03:36,300 --> 00:03:37,740
It's probably not the best name, is it?

60
00:03:37,770 --> 00:03:40,470
Whatever should name it something a little bit cleverer than that.

61
00:03:40,470 --> 00:03:41,700
But this is our variable.

62
00:03:41,700 --> 00:03:47,070
What you might see if you're cruising around on the interwebs, you'll see a lot of people use public

63
00:03:47,070 --> 00:03:49,530
instead of using serialized field.

64
00:03:49,530 --> 00:03:54,420
That is because if we set this to public, I'm just going to save my script here, jump back to Unity

65
00:03:54,420 --> 00:03:56,190
and show you what I'm talking about.

66
00:03:56,190 --> 00:04:00,570
Public is another way to allow us to see this in the inspector.

67
00:04:00,570 --> 00:04:05,130
So if I click on Barry Fine crash detector, you can see here we've got whatever set to ten.

68
00:04:05,130 --> 00:04:06,660
I could modify that in here.

69
00:04:06,660 --> 00:04:13,830
So the public keyword or the public access modifier is another way to allow us to edit in our inspector.

70
00:04:13,830 --> 00:04:20,070
However, I choose not to do it that way because this also means we can modify whatever from other scripts

71
00:04:20,070 --> 00:04:24,570
and that can happen accidentally or it can be something that we modify, change, update and we don't

72
00:04:24,570 --> 00:04:26,220
quite know exactly how it's happening.

73
00:04:26,220 --> 00:04:29,820
So that is why Serialize Field is a safer way of going about doing that.

74
00:04:29,820 --> 00:04:33,510
Now in our situation here, what is it that we would like to make public?

75
00:04:33,510 --> 00:04:39,690
And I think we would like to have something within our player controller that is made public that says

76
00:04:39,690 --> 00:04:41,670
anyone or any script.

77
00:04:41,670 --> 00:04:43,410
I should say the script is not a person.

78
00:04:43,410 --> 00:04:44,280
Rick, calm down.

79
00:04:44,280 --> 00:04:51,060
So anywhere within our code base, within our project, we can call something on player controller to

80
00:04:51,060 --> 00:04:55,020
say, you can't move anymore, we're disabling controls.

81
00:04:55,020 --> 00:04:56,940
Let's go about creating that framework.

82
00:04:56,940 --> 00:04:58,650
I'm going to pop underneath update.

83
00:04:58,650 --> 00:05:02,580
We will add in a new method and I'm going to call this method.

84
00:05:02,710 --> 00:05:06,070
Disable controls because that's what it's doing now.

85
00:05:06,070 --> 00:05:11,560
In front of that, we're going to use our keyword public and we need to say what the return type is

86
00:05:11,560 --> 00:05:13,390
and we don't have any return information.

87
00:05:13,390 --> 00:05:14,290
We're looking to get back.

88
00:05:14,290 --> 00:05:19,930
So we add void in here, we add our parentheses as usual, create our curly braces.

89
00:05:19,930 --> 00:05:26,800
And what is it that we want to have happen when we when someone says, hey, disable the controls,

90
00:05:26,800 --> 00:05:27,460
man.

91
00:05:27,460 --> 00:05:31,390
Well, we want to literally not allow the player to do these things.

92
00:05:31,390 --> 00:05:36,850
So there's a way we can do that by setting ourselves up a ball and using an if statement jump up to

93
00:05:36,850 --> 00:05:39,700
the top of our script, we'll create a ball and we'll call.

94
00:05:39,700 --> 00:05:45,430
This can move and we'll initialize that at true because you should be able to move.

95
00:05:45,430 --> 00:05:46,600
Yes, you can move.

96
00:05:46,600 --> 00:05:48,790
Now, a little micro challenge for you here.

97
00:05:48,790 --> 00:05:54,940
I want you to go about using our ball to say that if you can move, then you can move.

98
00:05:54,940 --> 00:05:56,980
But if you can't move, then you can't move.

99
00:05:56,980 --> 00:06:00,430
So this is a micro challenge for you, see if you know how to do that.

100
00:06:02,700 --> 00:06:03,210
Okay.

101
00:06:03,240 --> 00:06:04,680
Hopefully that wasn't too vague.

102
00:06:04,690 --> 00:06:06,170
It kind of felt a little bit vague.

103
00:06:06,180 --> 00:06:08,700
But where we can do this is within update.

104
00:06:08,700 --> 00:06:14,510
So I can type in if can move and by default that means it's true.

105
00:06:14,520 --> 00:06:16,890
You don't have to say equals equals true.

106
00:06:16,890 --> 00:06:23,520
But just know if you're saying if can move is if this thing resolves to true, then hey man, you can

107
00:06:23,520 --> 00:06:28,470
go about rotating, it can go about boosting and then curly brace at the end.

108
00:06:28,470 --> 00:06:30,600
So if you can move, then go about moving.

109
00:06:30,600 --> 00:06:31,120
Great.

110
00:06:31,140 --> 00:06:36,600
However, what we want to do is put some code in here with our disable controls to change that.

111
00:06:36,600 --> 00:06:38,940
So another little micro challenge for you here.

112
00:06:38,940 --> 00:06:46,110
What code can we add to our disable controls method so that if it's called, you can no longer move?

113
00:06:47,820 --> 00:06:52,670
We will simply say can move the variable now equals false.

114
00:06:52,680 --> 00:06:55,530
What is flicking the switch on our ball so it's false.

115
00:06:55,530 --> 00:07:01,800
I'm going to save that and head over to crash detector, which is the place where we want to be calling

116
00:07:01,800 --> 00:07:03,930
our new fancy public method.

117
00:07:03,930 --> 00:07:07,170
And the first thing in here we need to say where the player controller is.

118
00:07:07,170 --> 00:07:11,610
We need a reference to the player controller so we could set up a serialized field and say, this is

119
00:07:11,610 --> 00:07:13,560
the particular player controller.

120
00:07:13,560 --> 00:07:14,520
We could bring that in.

121
00:07:14,520 --> 00:07:19,320
We can manually, manually drag it in the inspector, but we've just learned how to use find objective

122
00:07:19,320 --> 00:07:19,590
type.

123
00:07:19,600 --> 00:07:21,360
So let's use that one more time.

124
00:07:21,360 --> 00:07:26,760
I'm going to under my if statement here, if the thing that I bumped into is the ground I'm going to

125
00:07:26,760 --> 00:07:30,930
find object of type, what type do you think it's going to be?

126
00:07:31,110 --> 00:07:37,290
It's going to be the player controller, which is the script, which is a class, but it's also a type

127
00:07:37,290 --> 00:07:38,190
dot.

128
00:07:38,190 --> 00:07:39,930
And we're going to call something in here.

129
00:07:39,930 --> 00:07:41,100
What will we call?

130
00:07:41,100 --> 00:07:44,040
You can see it listed right out there in all its glory.

131
00:07:44,040 --> 00:07:49,740
This method we've created, which is public disable controls, parentheses, semicolon.

132
00:07:49,740 --> 00:07:52,560
So we're hereby calling a public method.

133
00:07:52,560 --> 00:07:57,630
I'm going to save that and just show you very quickly if I was to jump back in here and remove this

134
00:07:57,630 --> 00:08:03,960
public keyword from disabled controls, save head back to crash detector, it's now like, hey, you

135
00:08:03,960 --> 00:08:04,770
can't do this.

136
00:08:04,770 --> 00:08:09,000
This is inaccessible due to its protection level, which is cool.

137
00:08:09,000 --> 00:08:10,170
Shows that process.

138
00:08:10,170 --> 00:08:13,410
So put public back in here, save it once again.

139
00:08:13,410 --> 00:08:14,910
Crash detector should be cool.

140
00:08:14,910 --> 00:08:16,770
Let us jump into unity.

141
00:08:16,770 --> 00:08:20,520
Click on play and see if we can rotate after we crash.

142
00:08:20,520 --> 00:08:24,570
So crash and I'm banging on the keyboard, but nothing is happening.

143
00:08:24,570 --> 00:08:25,110
Excellent.

144
00:08:25,110 --> 00:08:28,470
So it is successfully disabled our controls.

145
00:08:28,470 --> 00:08:32,580
So just to recap on that, I know it's a new concept, but it's fairly simple.

146
00:08:32,580 --> 00:08:38,309
Once you use it a few times when we have the public keyword, it simply means that we can access methods

147
00:08:38,309 --> 00:08:41,970
from that class in other classes.

148
00:08:41,970 --> 00:08:45,360
We can also do that for variables, but we tend not to do that.

149
00:08:45,360 --> 00:08:49,050
We tend not to make public variables because they can get a little bit dangerous.

150
00:08:49,050 --> 00:08:53,490
It's a lot harder to see if something's been changed as a variable than it is to see if something's

151
00:08:53,490 --> 00:08:55,140
being called as a method.

152
00:08:55,140 --> 00:08:55,860
Well, I think so.

153
00:08:55,860 --> 00:08:56,490
Anyway.

154
00:08:56,490 --> 00:08:57,210
So there we go.

155
00:08:57,210 --> 00:09:01,500
We've learned a new thing which is using the public access modifier keyword.

156
00:09:01,500 --> 00:09:03,120
I will see you in the next lecture.

