Getting started with Scripts – Unity Tutorial

In last article, we discussed about using GameObjects and prefabs. This article explains about how control GameObjects using scripts. Getting started with scripts in unity is quite easier as it requires C# or JavaScript programming language to edit the code.

Why Unity Scripts?

As we discussed in last article that a GameObject without any component is nothing but the empty container. Adding components to the GameObjects will add features, properties and functionality to it, like how it will look or how it will behave. Unity already provides many built-in components.

Unity scripts allows to create your own custom components. Using scripts, you can write your own custom feature for any GameObject. It also allows you to change the GameObjects functionality at run-time.

Getting Started with Scripts

Follow the below steps to create a script file and attach it to a GameObject.

Language Support: C#, JavaScript

Create Unity Script

  • Create scripts using Assets -> Create -> C# Script or JavaScript
  • Right click in the Project panel then Create -> C# Script or JavaScript
  • Click on Create menu at top left of the Project Panel and select C# script or JavaScript.

The new script will be created in the selected folder of the project panel. You can rename the script at the same time.

Getting started withs scripts - SampleScript


Open the Script File

Once the script file will be created, double click on the script to open it. By default, the script file will open in MonoDevelop editor but you can also edit it in the Visual Studio.

To change the default editor, navigate to Edit -> Preferences -> External Tools. Select your preferred choice of editor in External Script Editor menu.

Getting started with scripts - External Script Editor

Structure of the Script

Once you will open script file, it will look like below given code.

Key points:

  • UnityEngine Namespace is used in unity script file. This namespace is present in UnityEngine.dll.
  • Every unity script inherits MonoBehaviour class.
  • OnStart() function: This function will be used to initialize the variables in the script. It will be called before game play.
  • Update() function: This function will be called once per frame.
  • OnStart and Update functions will be called automatically, you don’t have to call them specifically.

Add a script to the GameObject

You don’t need to create the instance of the scripts using new keyword as we do in C# language for any class. In order to instantiate the script file, attach it to the GameObject.

  • Select the GameObject in Hierarchy panel.
  • Drag and drop script from project panel to the Inspector panel.

Getting started with scripts - AddScript

Print in Console Panel

You can log statement in the console panel using Debug.Log function. Edit the code in the script as below mentioned code.

Run the unity application, you will see the given string will be printed in the console panel.

Getting started with Scripts - PrintConsole

Get component using scripts

You can get already attached components to the GameObject using scripts. Use GetComponent generic function for the same. After getting the component you can also edit them.

Currently with cube object, default material is attached. You can see that in the inspector window.

Getting started with Scripts - Material

Let’s try to get Mesh Renderer component using script and try to print name of the material attached to it.

Run the application.

Getting started with scripts - Get Component Console

Add component using scripts

You can add other built-in components using scripts. Use AddComponent generic function to add components to the GameObject.

For example, let’s add particle system component to the GameObject.

Run the application:

Getting started with scripts - AddComponet

Edit variable in a script

You can edit the variable defined in script using unity editor. Just make the access modifier of the variable as public.

Now you will be able to see and edit that variable in the unity editor.

Getting started with scripts - Variable

Hope you get an idea about unity scripts. Please post your comments for queries and feedback. Thanks for reading.

Share Button
The following two tabs change content below.
Gyanendu Shekhar is a technology enthusiast. He loves to learn new technologies. His area of interest includes Microsoft technologies, Augmented reality, Virtual reality, unity3d and android development.

Gyanendu Shekhar

Gyanendu Shekhar is a technology enthusiast. He loves to learn new technologies. His area of interest includes Microsoft technologies, Augmented reality, Virtual reality, unity3d and android development.

One thought on “Getting started with Scripts – Unity Tutorial

  • August 19, 2017 at 8:42 am

    Nice work Gyanendu !!


Leave a Reply

Your email address will not be published. Required fields are marked *