Hello again.
In this lecture, we're going to write our first piece of code in Visual Studio code, and we're going
to print it out to the console, a witty, funny statement that our character might be saying in our
platform a game.
So let's get started.
First step in this process is to make sure that Unity knows that the correct ID is the one we're looking
at.
So I'm going to go up to edit and then find preferences down near the bottom, a few up from the bottom.
Actually, in here we're looking for over on the left hand side, you'll see external tools.
Within external tools, you can see the external script editor, the very first option up there.
Make sure it says whichever editor you're using, I'm using Visual Studio code.
That means that any time we say, Hey, open one of our scripts, it will use Visual Studio code to
open it.
So that's what we'll do next in the project area.
So in the project tab down here, this big open space, I'm going to right click and click on Create
C-sharp script.
Now before you click on C-sharp script, just watch what I'm doing here because there's an important
lesson here.
So I'm going to do this watch for a second.
You can see it's highlighted our new behavior, something in there can't see the rest of it.
But before I click anywhere else, I'm going to type in my name, which will be print to console.
That's the name of my script.
So print to console and I'm using camel case.
That's the convention.
So a capital P, capital T, capital C or upper case PTC for print to console and hit enter.
Now if I click on this, my script is here.
You can see we get a little preview within unity.
It's not super useful.
We can't edit it in here.
We can't do anything with it.
That's what we need our ID for.
But this will allow us just to see what's in this script.
And you can see it says public class print to console.
We'll get into what classes are in the future, but for now we just need to know this has printed console.
Let me show you what can go wrong if you click elsewhere while you're doing this so you don't need to
follow along in this particular bit.
Right click Create C-sharp script.
Now if I click to the anywhere else, it will bake in New Behavior script.
And if I go to change this name in the future, so change it to something else.
When I click on my script, see the preview, you can see it says Still Says New Behavior script.
The fact that the class name new behavior script is different from the file name means that we're going
to get all sorts of issues.
I just wanted to show you that whole process because I know sometimes we accidentally click somewhere
else and then you get all these errors and you're not sure why.
So I'm going to delete my something else script.
And if you haven't done so already, create your script.
So right click create C-sharp script and then call it print to console and then just click on it to
double check that the class name print console matches the file name print to console.
Excellent.
Now I'm going to double click on that.
And what that should do is open up Visual Studio code or whichever idea you said you're using.
Let me just rearrange this to get a little bit nicely presented on the screen.
Okay.
You can make your font larger or smaller control minus or control plus.
So I think that's the right size for us together here.
And you can see we've got a whole bunch of stuff over on the left and a bunch of stuff in the middle.
You'll understand a lot of this stuff as time goes on.
For now, I just want to draw your attention to a couple of things.
The first thing for us to use Visual Studio code effectively, we need to add a couple of extensions.
So if you click on this bottom icon on the left hand toolbar here, you can see we've got extensions
we can search for.
I'll give myself a little bit more space by moving this middle line over.
I've already installed the two extensions that we need.
So if you're a bit of a power user, find those, install those and you're good to go.
The way you find them is up in our search box here.
I'm going to search for C sharp and that should show me all my options.
Look for the one that's by Microsoft.
Click on that.
Then over on here you'll see an install button.
I've already installed it.
So mine says uninstall.
You should say install.
So install that it should be a very quick, quick process and then also search for unity.
And that should show us the unity code snippets, this one here by Kleber Silva.
And that once again is a click install.
So I'm going to remove what I've got in my search.
And you can see I've got my two extensions installed here.
Why do we need extensions?
Well, these are the things that give us the cleverness, the autocomplete and the hints and all those
kind of things about what we're doing within our code.
So where is our code gone?
Well, you can see we've got a couple of tabs up here, just like in a browser.
I don't need to have this last tab, so I'm going to find my print to console CSS.
There's a lot more information in here.
There's a lot more heavy lifting or heavy power that we can look at in the future.
For example, if I click on the Explore tab, you can see that print to console is within my project
structure.
I've got my assets directory which matches my assets directory.
You can see underneath here obviously about unity hanging around that you can see.
But we won't worry about looking at all this at the moment because we're just going to be dealing in
pretty simple structures.
So I'm going to click on this one more time, the Explorer, and that will close it up.
So now we can just be paying attention to our code in here.
Okay.
So when we create a new script in Unity, there's some content that's automatically put in there.
For now, we don't need to dig into all of that.
I just want to draw your attention to one important area.
And this is start.
This is.
A function or a method that when we push the play button, let me just pop back to Unity.
This play button just here.
It will say play and do all the things we need to do in our game shows, what's going on through the
main camera into the game window.
And you can see I haven't told it to do anything yet.
I've just kind of got an image and stuff and I've actually got a little bit of an error down here because
I didn't do my layers correctly on my water.
Let me just quickly do that.
We'll put that at five.
Okay.
There we go.
So we talked about layers in the last lecture.
So it was underneath my ground.
But there it is.
We can see it now.
So not much is happening when I click the play button, but when I click the play button, start will
be cold.
So in other words, what will happen?
What we will do is make sure that Unity and Visual Studio Code are talking to each other properly.
So within start, just follow along with me here.
Trust me on this.
We're going to type debug with a capital D dot log log open my parentheses.
So these are the round brackets above the nine and zero on your keyboard.
And then we're going to use quotation marks just to the left of the enter key.
And we can type in here whatever we want to print to the console.
So I'll show you the console in just a moment.
We'll print in here.
I'm printing to the console a very inspired message.
And then down at the end of our statement, this is a statement here we're creating.
We need a semicolon.
Semicolon in C sharp is kind of like putting a full stop or a period when you're writing a sentence.
So we need our semicolon down the end to say, I'm done with this particular thing.
Now you can see there's a round dot up here on our tab print console.
If we do control SX to save now we get our cross mark so it shows we've now saved it.
We have to save it before this takes effect back over into unity.
So I've got my script and I've created my very first statement before.
This will do anything.
I need to put it into my game.
I need to attach it to a game object so that it happens.
Just sitting it down here in the assets directory, this is on the hard drive.
It's like, cool, I'm ready to go.
But for it to get in the game, literally get in the game, we need to put it somewhere.
So I'm going to click on Where's my dude's head?
There it is.
Got circled.
I'm just going to rename this to head just so I remember in the future.
So I just clicked over in the inspector.
Sorry, I did that quickly clicked in the inspector and just changed the name from circle to head.
You don't need to do that, but for me it's just so I remember where it is.
And now I'm going to grab my script and drag it into this big open area underneath the add component
button.
Let go and you can see print to console has now been added as a component to the head game object.
That means that it will run because it's in our scene.
And what is the console?
Well, the console is this tab over here, which allows us to print out information just for us, just
for game developers to look at.
The player will never see this, but it's a great place to show information about what's going on in
our game.
If we now click on the play button, you can see in our console it's going to print.
I'm printing to the console.
Excellent.
We've written some code, but that's a bit dull.
We can do better.
So I'm going to give this to you as a challenge to make sure it's all making sense.
Change the content of your print statement, of our print statement to something that a player might
be saying in your pretend platformer, for example.
For me, I'm going to change it to eight too many donuts to imply that he's not quite able to reach
that rope.
So there's your challenge.
Change the print statement to be something else and print it to the console.
When you've done that, I'll see you back here for my solution.
Okey dokey.
I'm going to find my debug log.
Just highlight all of this in here and say 8...2... many dot dot dot donuts.
Exclamation point.
Just for fun.
Save that.
Remember, you've got to save it for change.
Click back on here.
Sometimes unity is going to do a little bit of re compiling and thinking and updating stuff that's all
pretty normal and natural.
And look down in the console here.
When I click on play, there's not much going on with our game, but our guy is saying eight too many
donuts and that's the foundations of us writing code in Visual Studio code and then having unity, being
able to use that code when it's applied to a game object.
So there we go.
Great work in this lecture and I'll see you in the next one.