1
00:00:03,210 --> 00:00:03,690
Hello again.

2
00:00:03,690 --> 00:00:07,710
In this lecture, we're going to write our first piece of code in Visual Studio code, and we're going

3
00:00:07,710 --> 00:00:12,120
to print it out to the console, a witty, funny statement that our character might be saying in our

4
00:00:12,120 --> 00:00:12,990
platform a game.

5
00:00:12,990 --> 00:00:14,240
So let's get started.

6
00:00:14,250 --> 00:00:20,430
First step in this process is to make sure that Unity knows that the correct ID is the one we're looking

7
00:00:20,430 --> 00:00:20,580
at.

8
00:00:20,580 --> 00:00:25,410
So I'm going to go up to edit and then find preferences down near the bottom, a few up from the bottom.

9
00:00:25,410 --> 00:00:30,720
Actually, in here we're looking for over on the left hand side, you'll see external tools.

10
00:00:30,720 --> 00:00:36,180
Within external tools, you can see the external script editor, the very first option up there.

11
00:00:36,180 --> 00:00:40,020
Make sure it says whichever editor you're using, I'm using Visual Studio code.

12
00:00:40,020 --> 00:00:44,940
That means that any time we say, Hey, open one of our scripts, it will use Visual Studio code to

13
00:00:44,940 --> 00:00:45,330
open it.

14
00:00:45,330 --> 00:00:48,990
So that's what we'll do next in the project area.

15
00:00:48,990 --> 00:00:54,450
So in the project tab down here, this big open space, I'm going to right click and click on Create

16
00:00:54,480 --> 00:00:55,590
C-sharp script.

17
00:00:55,590 --> 00:00:59,640
Now before you click on C-sharp script, just watch what I'm doing here because there's an important

18
00:00:59,640 --> 00:01:00,150
lesson here.

19
00:01:00,150 --> 00:01:01,890
So I'm going to do this watch for a second.

20
00:01:01,890 --> 00:01:06,240
You can see it's highlighted our new behavior, something in there can't see the rest of it.

21
00:01:06,240 --> 00:01:13,350
But before I click anywhere else, I'm going to type in my name, which will be print to console.

22
00:01:13,530 --> 00:01:15,390
That's the name of my script.

23
00:01:15,390 --> 00:01:17,670
So print to console and I'm using camel case.

24
00:01:17,670 --> 00:01:18,510
That's the convention.

25
00:01:18,510 --> 00:01:25,260
So a capital P, capital T, capital C or upper case PTC for print to console and hit enter.

26
00:01:25,500 --> 00:01:28,260
Now if I click on this, my script is here.

27
00:01:28,260 --> 00:01:30,990
You can see we get a little preview within unity.

28
00:01:30,990 --> 00:01:31,890
It's not super useful.

29
00:01:31,890 --> 00:01:33,060
We can't edit it in here.

30
00:01:33,060 --> 00:01:34,320
We can't do anything with it.

31
00:01:34,320 --> 00:01:36,300
That's what we need our ID for.

32
00:01:36,300 --> 00:01:39,120
But this will allow us just to see what's in this script.

33
00:01:39,120 --> 00:01:42,750
And you can see it says public class print to console.

34
00:01:42,750 --> 00:01:46,890
We'll get into what classes are in the future, but for now we just need to know this has printed console.

35
00:01:46,890 --> 00:01:51,720
Let me show you what can go wrong if you click elsewhere while you're doing this so you don't need to

36
00:01:51,720 --> 00:01:53,040
follow along in this particular bit.

37
00:01:53,040 --> 00:01:55,380
Right click Create C-sharp script.

38
00:01:55,380 --> 00:02:00,000
Now if I click to the anywhere else, it will bake in New Behavior script.

39
00:02:00,000 --> 00:02:05,250
And if I go to change this name in the future, so change it to something else.

40
00:02:05,790 --> 00:02:11,610
When I click on my script, see the preview, you can see it says Still Says New Behavior script.

41
00:02:11,610 --> 00:02:16,680
The fact that the class name new behavior script is different from the file name means that we're going

42
00:02:16,680 --> 00:02:18,180
to get all sorts of issues.

43
00:02:18,180 --> 00:02:21,840
I just wanted to show you that whole process because I know sometimes we accidentally click somewhere

44
00:02:21,840 --> 00:02:24,270
else and then you get all these errors and you're not sure why.

45
00:02:24,270 --> 00:02:26,430
So I'm going to delete my something else script.

46
00:02:26,430 --> 00:02:29,220
And if you haven't done so already, create your script.

47
00:02:29,220 --> 00:02:35,040
So right click create C-sharp script and then call it print to console and then just click on it to

48
00:02:35,040 --> 00:02:40,470
double check that the class name print console matches the file name print to console.

49
00:02:40,470 --> 00:02:41,040
Excellent.

50
00:02:41,490 --> 00:02:43,650
Now I'm going to double click on that.

51
00:02:43,650 --> 00:02:48,990
And what that should do is open up Visual Studio code or whichever idea you said you're using.

52
00:02:49,020 --> 00:02:52,710
Let me just rearrange this to get a little bit nicely presented on the screen.

53
00:02:53,160 --> 00:02:53,490
Okay.

54
00:02:53,490 --> 00:02:57,990
You can make your font larger or smaller control minus or control plus.

55
00:02:57,990 --> 00:03:00,600
So I think that's the right size for us together here.

56
00:03:00,600 --> 00:03:04,500
And you can see we've got a whole bunch of stuff over on the left and a bunch of stuff in the middle.

57
00:03:04,500 --> 00:03:07,230
You'll understand a lot of this stuff as time goes on.

58
00:03:07,230 --> 00:03:09,750
For now, I just want to draw your attention to a couple of things.

59
00:03:09,750 --> 00:03:15,540
The first thing for us to use Visual Studio code effectively, we need to add a couple of extensions.

60
00:03:15,540 --> 00:03:21,150
So if you click on this bottom icon on the left hand toolbar here, you can see we've got extensions

61
00:03:21,150 --> 00:03:22,170
we can search for.

62
00:03:22,170 --> 00:03:25,890
I'll give myself a little bit more space by moving this middle line over.

63
00:03:25,890 --> 00:03:29,190
I've already installed the two extensions that we need.

64
00:03:29,190 --> 00:03:33,000
So if you're a bit of a power user, find those, install those and you're good to go.

65
00:03:33,030 --> 00:03:35,640
The way you find them is up in our search box here.

66
00:03:35,640 --> 00:03:39,090
I'm going to search for C sharp and that should show me all my options.

67
00:03:39,090 --> 00:03:40,680
Look for the one that's by Microsoft.

68
00:03:40,680 --> 00:03:41,490
Click on that.

69
00:03:41,490 --> 00:03:43,590
Then over on here you'll see an install button.

70
00:03:43,590 --> 00:03:44,370
I've already installed it.

71
00:03:44,370 --> 00:03:45,810
So mine says uninstall.

72
00:03:45,840 --> 00:03:46,860
You should say install.

73
00:03:46,860 --> 00:03:52,380
So install that it should be a very quick, quick process and then also search for unity.

74
00:03:52,380 --> 00:03:57,690
And that should show us the unity code snippets, this one here by Kleber Silva.

75
00:03:57,690 --> 00:04:00,060
And that once again is a click install.

76
00:04:00,060 --> 00:04:01,710
So I'm going to remove what I've got in my search.

77
00:04:01,710 --> 00:04:04,350
And you can see I've got my two extensions installed here.

78
00:04:04,350 --> 00:04:05,490
Why do we need extensions?

79
00:04:05,490 --> 00:04:11,130
Well, these are the things that give us the cleverness, the autocomplete and the hints and all those

80
00:04:11,130 --> 00:04:14,130
kind of things about what we're doing within our code.

81
00:04:14,130 --> 00:04:15,480
So where is our code gone?

82
00:04:15,480 --> 00:04:19,079
Well, you can see we've got a couple of tabs up here, just like in a browser.

83
00:04:19,079 --> 00:04:23,400
I don't need to have this last tab, so I'm going to find my print to console CSS.

84
00:04:23,430 --> 00:04:25,020
There's a lot more information in here.

85
00:04:25,020 --> 00:04:28,500
There's a lot more heavy lifting or heavy power that we can look at in the future.

86
00:04:28,500 --> 00:04:34,710
For example, if I click on the Explore tab, you can see that print to console is within my project

87
00:04:34,710 --> 00:04:35,220
structure.

88
00:04:35,220 --> 00:04:38,160
I've got my assets directory which matches my assets directory.

89
00:04:38,160 --> 00:04:41,490
You can see underneath here obviously about unity hanging around that you can see.

90
00:04:41,490 --> 00:04:44,790
But we won't worry about looking at all this at the moment because we're just going to be dealing in

91
00:04:44,790 --> 00:04:46,050
pretty simple structures.

92
00:04:46,050 --> 00:04:49,620
So I'm going to click on this one more time, the Explorer, and that will close it up.

93
00:04:49,620 --> 00:04:52,680
So now we can just be paying attention to our code in here.

94
00:04:52,680 --> 00:04:52,950
Okay.

95
00:04:52,950 --> 00:04:56,910
So when we create a new script in Unity, there's some content that's automatically put in there.

96
00:04:56,910 --> 00:04:58,830
For now, we don't need to dig into all of that.

97
00:04:58,830 --> 00:05:01,260
I just want to draw your attention to one important area.

98
00:05:01,260 --> 00:05:02,280
And this is start.

99
00:05:02,280 --> 00:05:02,630
This is.

100
00:05:02,760 --> 00:05:09,060
A function or a method that when we push the play button, let me just pop back to Unity.

101
00:05:09,060 --> 00:05:10,650
This play button just here.

102
00:05:10,650 --> 00:05:16,290
It will say play and do all the things we need to do in our game shows, what's going on through the

103
00:05:16,290 --> 00:05:18,360
main camera into the game window.

104
00:05:18,360 --> 00:05:20,490
And you can see I haven't told it to do anything yet.

105
00:05:20,490 --> 00:05:24,270
I've just kind of got an image and stuff and I've actually got a little bit of an error down here because

106
00:05:24,270 --> 00:05:27,930
I didn't do my layers correctly on my water.

107
00:05:27,930 --> 00:05:29,100
Let me just quickly do that.

108
00:05:29,100 --> 00:05:30,120
We'll put that at five.

109
00:05:30,120 --> 00:05:30,450
Okay.

110
00:05:30,450 --> 00:05:31,170
There we go.

111
00:05:31,170 --> 00:05:33,120
So we talked about layers in the last lecture.

112
00:05:33,120 --> 00:05:35,010
So it was underneath my ground.

113
00:05:35,010 --> 00:05:35,490
But there it is.

114
00:05:35,490 --> 00:05:36,420
We can see it now.

115
00:05:36,420 --> 00:05:41,550
So not much is happening when I click the play button, but when I click the play button, start will

116
00:05:41,550 --> 00:05:42,420
be cold.

117
00:05:42,420 --> 00:05:43,860
So in other words, what will happen?

118
00:05:43,860 --> 00:05:48,930
What we will do is make sure that Unity and Visual Studio Code are talking to each other properly.

119
00:05:48,930 --> 00:05:51,480
So within start, just follow along with me here.

120
00:05:51,480 --> 00:05:52,380
Trust me on this.

121
00:05:52,380 --> 00:05:59,010
We're going to type debug with a capital D dot log log open my parentheses.

122
00:05:59,010 --> 00:06:03,780
So these are the round brackets above the nine and zero on your keyboard.

123
00:06:03,780 --> 00:06:07,410
And then we're going to use quotation marks just to the left of the enter key.

124
00:06:07,410 --> 00:06:11,550
And we can type in here whatever we want to print to the console.

125
00:06:11,550 --> 00:06:13,260
So I'll show you the console in just a moment.

126
00:06:13,260 --> 00:06:14,250
We'll print in here.

127
00:06:14,250 --> 00:06:19,110
I'm printing to the console a very inspired message.

128
00:06:19,110 --> 00:06:23,460
And then down at the end of our statement, this is a statement here we're creating.

129
00:06:23,460 --> 00:06:24,780
We need a semicolon.

130
00:06:24,780 --> 00:06:29,640
Semicolon in C sharp is kind of like putting a full stop or a period when you're writing a sentence.

131
00:06:29,640 --> 00:06:33,210
So we need our semicolon down the end to say, I'm done with this particular thing.

132
00:06:33,210 --> 00:06:37,290
Now you can see there's a round dot up here on our tab print console.

133
00:06:37,290 --> 00:06:43,290
If we do control SX to save now we get our cross mark so it shows we've now saved it.

134
00:06:43,290 --> 00:06:47,220
We have to save it before this takes effect back over into unity.

135
00:06:47,220 --> 00:06:49,860
So I've got my script and I've created my very first statement before.

136
00:06:49,860 --> 00:06:50,850
This will do anything.

137
00:06:50,850 --> 00:06:52,650
I need to put it into my game.

138
00:06:52,650 --> 00:06:56,070
I need to attach it to a game object so that it happens.

139
00:06:56,070 --> 00:06:59,610
Just sitting it down here in the assets directory, this is on the hard drive.

140
00:06:59,610 --> 00:07:01,170
It's like, cool, I'm ready to go.

141
00:07:01,170 --> 00:07:04,920
But for it to get in the game, literally get in the game, we need to put it somewhere.

142
00:07:04,920 --> 00:07:07,740
So I'm going to click on Where's my dude's head?

143
00:07:07,740 --> 00:07:08,460
There it is.

144
00:07:08,490 --> 00:07:09,150
Got circled.

145
00:07:09,150 --> 00:07:12,630
I'm just going to rename this to head just so I remember in the future.

146
00:07:12,630 --> 00:07:14,400
So I just clicked over in the inspector.

147
00:07:14,400 --> 00:07:18,780
Sorry, I did that quickly clicked in the inspector and just changed the name from circle to head.

148
00:07:18,780 --> 00:07:21,840
You don't need to do that, but for me it's just so I remember where it is.

149
00:07:21,840 --> 00:07:28,200
And now I'm going to grab my script and drag it into this big open area underneath the add component

150
00:07:28,200 --> 00:07:28,530
button.

151
00:07:28,530 --> 00:07:35,580
Let go and you can see print to console has now been added as a component to the head game object.

152
00:07:35,580 --> 00:07:38,490
That means that it will run because it's in our scene.

153
00:07:38,490 --> 00:07:39,450
And what is the console?

154
00:07:39,450 --> 00:07:44,580
Well, the console is this tab over here, which allows us to print out information just for us, just

155
00:07:44,580 --> 00:07:45,990
for game developers to look at.

156
00:07:45,990 --> 00:07:50,340
The player will never see this, but it's a great place to show information about what's going on in

157
00:07:50,340 --> 00:07:50,940
our game.

158
00:07:50,940 --> 00:07:55,770
If we now click on the play button, you can see in our console it's going to print.

159
00:07:55,770 --> 00:07:57,540
I'm printing to the console.

160
00:07:57,540 --> 00:07:57,930
Excellent.

161
00:07:57,930 --> 00:08:00,780
We've written some code, but that's a bit dull.

162
00:08:00,780 --> 00:08:01,470
We can do better.

163
00:08:01,470 --> 00:08:04,440
So I'm going to give this to you as a challenge to make sure it's all making sense.

164
00:08:04,440 --> 00:08:09,090
Change the content of your print statement, of our print statement to something that a player might

165
00:08:09,090 --> 00:08:11,520
be saying in your pretend platformer, for example.

166
00:08:11,520 --> 00:08:16,410
For me, I'm going to change it to eight too many donuts to imply that he's not quite able to reach

167
00:08:16,410 --> 00:08:17,010
that rope.

168
00:08:17,010 --> 00:08:18,510
So there's your challenge.

169
00:08:18,540 --> 00:08:21,720
Change the print statement to be something else and print it to the console.

170
00:08:21,720 --> 00:08:24,960
When you've done that, I'll see you back here for my solution.

171
00:08:27,320 --> 00:08:28,220
Okey dokey.

172
00:08:28,220 --> 00:08:31,250
I'm going to find my debug log.

173
00:08:31,250 --> 00:08:37,960
Just highlight all of this in here and say 8...2... many dot dot dot donuts.

174
00:08:37,970 --> 00:08:39,200
Exclamation point.

175
00:08:39,200 --> 00:08:39,890
Just for fun.

176
00:08:39,890 --> 00:08:40,789
Save that.

177
00:08:40,789 --> 00:08:42,440
Remember, you've got to save it for change.

178
00:08:42,440 --> 00:08:43,309
Click back on here.

179
00:08:43,309 --> 00:08:48,290
Sometimes unity is going to do a little bit of re compiling and thinking and updating stuff that's all

180
00:08:48,290 --> 00:08:49,340
pretty normal and natural.

181
00:08:49,340 --> 00:08:50,660
And look down in the console here.

182
00:08:50,660 --> 00:08:55,910
When I click on play, there's not much going on with our game, but our guy is saying eight too many

183
00:08:55,910 --> 00:09:02,060
donuts and that's the foundations of us writing code in Visual Studio code and then having unity, being

184
00:09:02,060 --> 00:09:05,960
able to use that code when it's applied to a game object.

185
00:09:05,960 --> 00:09:06,650
So there we go.

186
00:09:06,680 --> 00:09:09,110
Great work in this lecture and I'll see you in the next one.

