Basics of Scripting in Unity

Jan, 2018

- By Game Art & Design Department of ICAT Design and Media College

The Unity game engine offers us in-built game objects and components to ease our game development process. It also provides 2D Physics and 3D Physics components like colliders for interaction of the game objects in our game. But are they just enough to create a ‘workable’ game? What will make a game playable?
As a Game Developer, you should be able to write specific behaviours of your own for every component you introduce to a game. You can do it by writing scripts. This blog gives you an introduction to scripting in Unity.

 

How Scripting Works in Unity?
Generally, scripting tells our game objects ‘how to behave’, and it is the script that defines the game objects and how they interact with each other. It creates the game play.
But in Unity, scripting is different from that in software application development. This is because, Unity engine performs execution in a big loop; what exactly it does is: it reads the entire data in the scene, which includes game objects, assets, different components attached with every game object and their appropriate behaviour, and then it renders a frame. In Unity, the script is attached to the game object to render any actions of it in every frame.
 “Scripting uses Logics, not Maths." Many people think this is for those who are versed in Math. No, it’s not about Math, it is about Logics. It is about how to drive code that’s in-built in Unity; the code that is prebuilt into Unity is what the Unity 3D Game Engine itself, and it is accessed through an Application Programme Interface (API). We can find the API reference from the predefined Unity library.
The scripts in Unity are written in a special language that Unity can understand and it is how we instruct Unity with codes. Unity supports variety of scripting languages, which are Object Oriented Scripting Languages.
One of most commonly used languages is C# (C Sharp). Like any other language, scripting language also has some syntax and basic parts of speech, such as Variable, Classes and Objects.

 

Creating a C# Script
There are many ways to create scripts in Unity. Let’s see one of the ways of creating it.

  • In Unity, in the ‘Project’ window, select 'Create'.
  • Then, select ‘C# Script’. This will create a default C# file, "NewBehaviourScript.cs" in the target location.
  • Fig 1.1

  • Double-click the file created to open the script in Text Editor, where you can create the script.

There are many external tools to edit the script created in Unity. By default, Unity provides a text editor called as ‘Mono Develop’, which is bundled with Unity. It’s a powerful editor for Unity Programming and it is also called as Developer’s Environment.

Mono Develop gives us a lot of useful suggestions which help in completing our code. If the programmers are wrong at some instance, it helps in rectifying the mistake.


Basic Anatomy of C# Program:

Variables are the instances which exist throughout the execution of scripts. It holds the data like speed, jump, and weight etc in the game. A variable’s name starts with a lowercase letter or underscore. Unity follows Pascal naming conventions.
Syntax:  public type variable_name;    Example: public float speed;
Here, ‘type’ can be anything like camera, rigidbody, color, int, float, bool etc.
‘variable_name’ can be any user defined names for representing the data.
Functions are the blocks enclosed within flower braces { }. It helps in reducing the number of lines in the script, as it can be invoked multiple times, when called. A function’s name starts with an uppercase letter.
Start ( ) is a predefined method used in Unity. In C# scripting, this method is used for initializing variables. This method runs at the beginning of the script execution. Start ( ) calls before Update ( ) in MonoBehaviour.
Update ( ) is a predefined method used in Unity. The Update method is called in every frame, only if the MonoBehaviour is enabled. Update is the most commonly used function to implement any kind of game behaviour. Update ( ) calls after Start ( ) in MonoBehaviour.
FixedUpdate ( ) should be used instead of Update ( ) when dealing with Rigidbody. For example, when adding a force to a rigidbody, you have to apply the force in every fixed frame inside FixedUpdate () instead of every frame inside Update ( ).
LateUpdate ( ) is called in every frame, only if the MonoBehaviour is enabled. LateUpdate () is called after all Update ( ) functions have been called. This is useful to arrange script execution.
For example, a ‘follow camera’ should always be implemented in LateUpdate ( ) because it tracks object that might have moved inside Update ( ).

 

Let’s consider an example for better understanding:
Here’s a simple C# script attached with game object to switch On and switch Off a light.

A script called DemoScript.cs is being attached with ‘Light’ game object  as show in the Inspector (refer Fig. 1.2).
Within Update ( ), method there is an if condition Input.GetKey (“space”) telling that when a user presses a spacebar in the keyboard, the statement myLight.enabled = true will enable the light property true and the light turns On in the game window; else the statement myLight.enabled = false will make the light property false and the light goes Off.

Fig 1.2

Fig 1.3

As shown in fig 1.2 and fig 1.3, we can attach the script with game object and achieve simple game events.

Conclusion:
Unity Engine provides several ways to interact with its game objects and assets by attaching scripts written with logics. As scripting in unity is made easier by implementing Object Oriented Programming techniques and as Unity provides built-in Text Editor called MonoDevelop for editing the scripts, it is easy for you to try your ideas through scripting.
Try your hands on scripting and develop the games in your dream.