00. First Script

Using Unity 2021.3.33f1 and Visual Scripting 1.8.0. The project is using the 2D Core template.

Scripts are just text documents. Special text editors known as IDEs are typically used for writing code. Common IDEs include Visual Studio (free) and Rider (paid). The benefits of an IDE is that they can highlight errors, offer suggestions, and help navigate code.

You can create a new script in Unity by:

  1. Right-clicking anywhere in the Project window and choosing Create > C# Script.
  2. Alternatively, you can select Assets > Create > C# Script from the top menu.

Below is a screenshot of Rider with a default script from Unity.

Rider

The default script

Let’s try to create a new C# script that we call Player. You can open it by double-clicking the script. New scripts in Unity are created with some standard content. The new script should look like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Player : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        
    }
}

If you prefer to just get your hands dirty, then you can skip ahead to 01. A Simple Player. But please keep on reading, if you would like a little more background knowledge.

Using

The first three lines all begin with the word using. You can ignore these for now, but leave them in! The short explanation is that they tell the computer that you want to use some specific libraries of code. UnityEngine is the library that contains many of the classes and methods that we want to use in our scripts.

1
2
3
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

Classes and components

One thing you may notice is that the word Player appears on line 5. That is exactly the name we chose for our script, and it is also the name of our class.

A class is just a feature of the C# programming language that allows us to organise our code. You can think of it as grouping together a lot of data and functionality into a neat package.

The most simple class will look like the following. Did you notice the opening and closing curly braces { }? They mark the beginning and end of the class. All code must be placed in between these curly braces.

1
2
3
4
public class SuperSimple
{
    // This is just a code comment
}

Since we want our class to be used as a component on game objects in Unity, we must add the : MonoBehaviour part after the class name. The colon is the syntax for extending an existing class. MonoBehaviour happens to be the class that contains a lot of functionality needed to work with objects in Unity.

Components are the building blocks for our game logic. Below is a screenshot of our Player component, assigned to a game object. You can assign components by dragging your script onto a game object in the Hierarchy or clicking the Add Component button in the Inspector.

component

Methods

The default script contains two methods. A method is a series of instructions packed together, like an algorithm or the recipe for blueberry muffins.

Methods can be recognised by the parentheses that follow their name (). They contain the functionality of our script. There are many existing methods that we can use, but we can also write our own.

Unity has a number of “magic” methods that run at specific times in our game:

  • Start() is called only once on the first frame. This is a good place to set up things.
  • Update() is called every frame repeatedly. This is a good place for logic that receives input, creates movement, or other kinds of changes that happen over time.

There are many more “magic” methods, but we will look at these when needed.

1
2
3
4
5
6
7
8
9
void Start()
{
    
}

void Update()
{
    
}

Naming

Naming scripts (and thereby classes) can be categorised into two groups: requirements and conventions.

Requirements for naming include:

  • Must begin with a letter or an underscore.
  • Can only contain letters, numbers, and underscores.
  • Cannot have any spaces.
  • In Unity, the name of a script must match the name of the class.

Conventions include:

  • Scripts, classes, and methods use PascalCase, e.g. PlayerInventory instead of player_inventory.
  • Variables use camelCase, e.g. healthPoints.
  • Private variables begin with an underscore, e.g. _healthPoints.

More on methods, variables, and the meaning of “private” later.