Overview

This tutorial introduces the fundamentals of Unity scripting. You'll learn how to create and use a Unity script.


Deep-diving & How To…?

The primary way to add custom behavior to a GameObject is through a script. Unity scripts inherit from several engine classes that provide numerous attributes and functionality.

First, let’s create a new folder for scripts named Scripts, inside the Assets folder.

Right click on the Project folder and select Folder inside the Create menu.

image.png

image.png

image.png

Create a new Script

There are two ways to create a new script (you may take a look on both but execute only one, otherwise you’ll have 2 scripts):

Create Script Option (preferred)

Add Component button

<aside> ⚠️

Be careful when renaming scripts and their classes. The script filename must match the name of the class it contains to ensure everything works properly.

As an example, if my script is named CustomBehaviour, the class inside must be named CustomBehaviour as well.

Failing to do so can lead to unexpected behaviour and errors in the engine.

</aside>

Using the script

<aside> 💡

Make sure you have the External Script Editor correctly configured. Check it in Edit → Preferences → External Tools section.

image.png

In this tutorials, the Script Editor is Visual Studio 2022.

In this tutorials, the Script Editor is Visual Studio 2022.

</aside>

<aside> 💡

The script can be opened with a double click on the component in the Inspector window, through the asset in the Project window, and also through the options menu in the component.

20250410-1442-11.3047876.mp4

</aside>

This is the Unity Script template. You’ll notice two functions inside the script class definition. These are Unity Messages.