- [Instructor] Scripting Conventions and Syntax.
Coding syntax simply means the structure of the language,
and some conventions are essential
to learning to read and write code.
For example, the dot operator.
The dot operator is a full stop, or period,
seen between words within code.
It works like writing the line of an address.
For example, consider this line of code.
Here, we could consider Debug the country,
and Log the city.
We're drilling down into things that are within Debug,
Log being one of those elements.
One helpful feature that Visual Studio provides
is that when we type a dot,
IntelliSense will provide a list of options
for code completion.
This can help us to write code faster, avoid errors,
and also help us to learn what choices we have available.
Then, for example, in this part of the code,
transform is the country,
position is the city,
and x is the street within the city
we're trying to locate.
So the dot operator is effectively allowing you
to separate or access elements
of a compound item in Unity,
a compound item being something that contains many elements.
So, for example,
transform contains position, rotation, and scale.
So the dot operator is used to choose position,
and then position contains x, y, or z.
So we've chosen x by using the dot operator once again.
The next piece of syntax is the semicolon.
The semicolon is used to terminate statements,
which is why you'll always see it at the end of a line.
However, not all parts of code are statements.
For example, the opening and closing of a class declaration,
or the opening and closing of functions or if statements.
Anything using a curly brace does not need a semicolon
at the end of it.
Any statements within these,
for example, after the opening
or before the closing,
will end with a semicolon.
Let's talk about indenting.
Indenting is an important part of writing code
that's both presentable and easy to read.
Indenting code is not technically necessary,
but it allows you to read your code more easily,
as it's used to show the functional structure of your code.
The code you see here is already fully indented,
but if I outdent all of this code using Shift and Tab,
we can do it a step at a time.
So, to begin with my public class statement declaration,
it starts here and ends at the bottom,
so I know that everything within it needs indenting
by at least one.
I'm using the Tab key to do this.
Next, in my Start function,
which starts here on line seven and ends on line 14,
I have a few lines of code.
They all need indenting,
because they're within the Start function.
Then, finally, within my if statement,
I have a Debug.Log line of code,
which I'm going to indent as well.
I can visually trace a line going down the page
and spot where all my code blocks start and end,
simply because I've used indentation.
Generally speaking, Visual Studio should help you with this
and do it automatically.
But just in case you've got anything wrong,
or you've pressed Tab in the wrong place,
you should make sure
that all of your indentation makes sense,
especially if you're working with someone else on your code.
Finally, comments.
Comments can be used literally as a sound
to write a comment about a piece of code,
in order to leave yourself a note or a reminder.
You can write a single-line comment
by using a double forward slash,
or you can write a multiple-line comment
by starting with a forward slash followed by an asterisk,
and terminating with the opposite,
meaning an asterisk followed by a forward slash.
Within that, you can continue
to write as many lines as you want,
so comments can be used to leave yourself
or other coders a note.
But it can also be used
to disable parts of code temporarily.
For example, if I want to disable this if statement,
I can put in a multi-line comment starting before it
and terminating after it.
You can see that Visual Studio is there to help me see
that this part is disabled.
It changes the syntax coloring of this part,
showing me that it's not actually going to be executed.
So this will not be executed
until I remove the comment again.
The compiler, or Unity in this case,
effectively just ignores what's going on
within that comment.