C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Conventions and SyntaxThere are some rules used in writing a good working code. These rules are called conventions. Like, you need capital letters, full stops, and commas for a written sentence to make sense. The code has its syntax for the computer to understand what your code means. See this block of code: public class BasicSyntax : MonoBehaviour { void Start () { Debug.Log(transform.position.x); } } Curly Brackets {}Curly brackets are used to contain blocks of code inside specific functions. Programming statements like if statements include curly brackets, as they can contain further blocks of code. As such, functions will often have code nested within code, separated by sets of brackets {}. Dot Operator .In the above part of the code, the dot operator is placed in between the words, and it allows access to different elements and properties. In this code, Log is just one of the elements of Debug, and x is just one element of the position, which is itself just one element of transform, all of which are properties of debugging. For example, "transform.position.x" in this part of the code transform is the country, the 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, where 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 a position, and then the position contains x, y, or z. So we have chosen x by using the dot operator once again. Semi-Colon ;It is used to terminate the statements and will appear at the end of a line of code. However, not everything is a statement, functions, and class declarations do not require a semicolon terminator. It is very easy to forget to add the semicolon. IndentingIn unity, indenting is not necessary, but this will help make your code a lot more readable to the human eye. Instead of having all code starting at the left margin, when opening a function, the code inside the curly brackets should be indented one level. Likewise, any function inside this should be indented one level more, so you can visually see which code is part of which function. CommentsComments are the part of the code which has no technical function; it is just an explanation or description of the source code. It helps a developer to explain the logic of the code, to enter the details about the author or programmer, and to improve program readability. At run-time comment is ignored by the compiler. // is a single line comment. Simply put // at the start of the line, the compiler will know to skip over it when calculating the code. /* */ is a multi-line comment. This multi-line comment can also be used to disable chunks of code without deleting them when testing and debugging your code.
Next TopicIf Statements
|