You are on page 1of 3

Learning Unity – Basic Script Elements

CCE VGD/GP

Basic Script Elements

Lesson Highlights

Creating Scripts Anatomy of a Script File

Required Software

Unity (There is a free version of Unity available at: http://unity3d.com/get- unity/download?ref=personal)

Overview

The behaviors of GameObjects (the assets in your game) are controlled by the Components that are attached to them. There are many useful built-in Components, however you will soon find the need to build your own gamepl ay features. In this tutorial, we will look at creating your very own Components using scripts. Scripts allow you to trigger game events, modify Component properties over time, and respond to user input.

Unity supports both C# (Pronounced See-Sharp) and UnityScript (sometimes you’ll hear it called JavaScript, but it’s a rather heavily modified version of JavaScript).

Creating Scripts

Unlike most other assets, scripts are usually created in the Unity Editor directly. You can create a new script from the Create menu at the top left of the Project panel.

Before doing anything else, give the script a meaningful name, and hit Enter / Return to commit the new name. You can rename the script later, but best practices says to do it now, save yourself the trouble of a complicated re-name later, and keeping you from forgetting later.

Anatomy of a Script file

When you double-click or right-click and select Open, the script will be opened in a text editor. MonoDevelop is the default, however you can select any editor you like from the External Tools panel in Unity’s preferences.

Learning Unity – Basic Script Elements

Default Script

1.

using UnityEngine;

2.

using System.Collections;

3.

4.

public class MainPlayer : MonoBehaviour {

5.

// Use this for initialization

6.

void Start () {

7.

}

8.

// Update is called once per frame

9.

void Update () {

10.

}

11.

}

CCE VGD/GP

The default script is shown above. Let’s take a look at the elements inside the script.

1.

using UnityEngine;

2.

using System.Collections;

Using Directives are used to import Namespaces (keywords). These go at the very top of the file, before any declarations.

3.

4.

public class MainPlayer : MonoBehaviour {

A class is a construct that enables you to create your own custom types by grouping together variables of other types, methods and events. A class is basically a blueprint. It defines the data and behavior of a type.

Because the access level is set to public, anyone can create objects from this class. The name of the class follows the class keyword. Remember, in Unity, when using C#, the class name must match the file name.

MonoBehaviour is the base class every script derives from, therefore this must be included in every script.

5.

// Use this for initialization

6.

void Start () {

When void is used as the return type for a method, void specifies the method doesn’t return a value. Basically, it’s a “nothing”.

Learning Unity – Basic Script Elements

CCE VGD/GP

There are a number of declarations for the execution order of event functions. That’s a fancy way of saying there are lots of ways to tell a piece of script when to run.

The most commonly used in building a game in Unity will be:

Start

Start is called before the first frame update only if the script instance is enabled. Update

Update is called once per frame. It is the main workhorse function for frame updates.

FixedUpdate

FixedUpdate is often called more frequently than Update. It can be called multiple times per frame, if the frame rate is low and it may not be called between frames at all if the frame rate is high. FixedUpdate is called on a reliable timer, independent of the frame rate.

LateUpdate

LateUpdate is called once per frame, after Update has finished. A common use for LateUpdate would be a following third-person camera. This will ensure that the character has moved completely before the camera tracks its position.

yield

The coroutine will continue after all Update functions have been called on the next frame.

OnDestroy

This function is called after all frame updates for the last frame of the object’s existence (the object might be destroyed in response to Object.Destroy or at the closure of a scene).

OnDisable

This function is called when the behaviour becomes disabled or inactive.

Learning Unity – Basic Script Elements CCE VGD/GP There are a number of declarations for the

Basic Script Elements by Levi Sterling is licensed under a Creative Commons Attribution 4.0 International License. This tutorial was written on Mac OSX, using Unity (Educational License), and Google Docs.