Tutorial – Unity: Scripting Conventions, Terminology and Syntax

Scripting in Unity can take the form of C# or Javascript. The following tutorials are all on C#, and are noted from the videos on Unity’s website.

SCRIPTS AS BEHAVIOUR COMPONENTS

A script can be applied to an object in unity just like any other component, and scripts can be created in the project panel, or in the toolbar. Scripts can be applied simply by dragging and dropping them onto an objects inspector.

This first tutorial will look at the basic anatomy of code, before further tutorials go more into detail about specific coding techniques.

 

USEFUL TERMINOLOGY

When discussing code or looking at other tutorials online it may be useful to know the terms for different parts of the code. To help visualise them, we will equate them to different parts of a factory.

Class: With reference to Unity, the Class is basically the Script which is applied to your gameobject. Think of this as the factory itself.

Function/Method: A specific block of code which carries out a particular task. Think of this as a machine within your factory.

Statement: A single line of code, ending with a semi colon. This is equivalent to the nuts and bolts that make up your factory machines.

Variable: A storage unit used to contain information which can be altered. This can contain either the raw materials, or the processed objects produced by the machines in your factory.

Parameter: Very similar to variables, often with the same types and uses, but they are used within functions to help generate an output.

 

FUNCTIONS (ALSO KNOWN AS METHODS)

Example: Void Start() {}

Functions are the main blocks of code in your scripts, with each function serving a different task. Think of your script as a factory, with each Function being a single machine inside it. For example, Start(){} occurs when your script first runs,  Update(){} occurs every frame. A function can take information inputs and give us different information back (called a return). Whilst Unity has certain specific built in Functions, you can also create your own ones – an example is shown below.

void Start(){ } is the function called when the object enters the scene.   Note the curly brackets, which all code will be contained inside. It is of type ‘void’ because it doesnt provide a return. This is also shown by it having nothing in the () part (the parameter).

If we wanted a return, we could for example have a function:

int MultiplyByTwo(int number) {}  

which breaks down as: type of function, name of function (type of parameter, name of parameter){}. This particular example will return an integer numerical value based on the code inside the {} section.

The last two pieces of information are the input – they determine the type of information you can feed the machine to get a result back – ‘number’ is  just a variable of type ‘int’.  In this case, we can insert an integer value as the parameter, or even another integer variable from elsewhere in the code. Note that void Start(){} had no parameter input, as we were expecting no return.

Elsewhere in our code, from other functions, we can call this function with the line MultiplyByTwo(score);

where ‘score’ is simply a pre-established integer variable.

WHY IS THIS USEFUL

Using functions within functions is at first daunting, but it serves a practical purpose. Say our script had 10 different functions, and each of them had at some point to do the same job as int MultiplyByTwo(int number) {}, which was itself 20 lines long. Rather than writing out the same code every time, which would be 200 lines of code, we simply write it once as a function, then have a single line call it from each other function that needs it.

This is cleaner and easier to read, but also makes editting and debugging code a much simpler process, as fewer lines have to be looked over.

 

VARIABLES

Variables are discussed in greater detail in the next tutorial, but common etiquette is to create all variables at the top of your script, outside of functions. This can however exclude any ‘temporary’ variables (parameters) held within Functions which are used as part of calculations.

SCRIPT CONVENTIONS AND SYNTAX

These are the conventions used in writing good working code. Same as you need capital letters, full stops and commas for a written sentence to make sense, code has its own syntax in order for the computer to understand what your code means. Take a look at the following code:

public class BasicSyntax : MonoBehaviour
{
void Start ()
{
Debug.Log(transform.position.x);}

}

Curly Brackets: {}

Are used to contain blocks of code inside specific functions. Certain types of statements, such as ‘if’ statements, contain 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. A common coding error is to have missing brackets so this should be one of the first things to look for when debugging.

Dot Operator: .

In the above code, the dot operator is the ‘.’ between words, and allows access to the different elements and properties inside a compund object. In this case, Log is just one of the elements of Debug, and x is just one element of position, which is itself just one element of transform, all of which are properties of debug. Transform has the other elements ‘rotation’ and ‘scale’.

Semi Colon: ;

Is used to terminate statements and will appear at the end of those lines of code. However, not everything is a statement. Functions and  Class declarations (anything that is followed by a set of curly brackets: {} ) do not require a semi colon terminator. It is very easy to forget to add the semi colon. If your script is showing an error, this is often the cause!

Indenting.

Although not technically necessary, this will help make your code a lot more readable to the human eye. Rather than 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 functions inside this should be indented one level more, so you can visually see which code is part of which function. This is demonstrated in the code above.

Comments.

Comments are parts of code which have no technical function, but merely leave a note which the human user can read. These are often left to explain functions etc.

The following is a single line comment. simply put a // at the start of the line and the computer will know to skip over it when calculating the code.

//hello

Next are the symbols for multi line commenting. Basically place each of these at the start and end of a longer piece of text to convert them into a comment.

/*

*/

This multi line method can also be used to disable chunks of code without deleting them, when testing and debugging your code. Most code editing software will convert comments to a green font colour to help you identify them easier.

 

 

 

 

 

 

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s