Unity Scripting Essentials
Unity Scripting Basics
Bringing Objects to Life with Code
So far, you've learned how to place objects in a scene. Now it's time to make them do things. In Unity, you give GameObjects behaviors by attaching scripts to them. A script is a file of code that acts as a set of instructions. Think of it like a recipe for a cake, but instead of baking, you're telling a cube how to move or a character how to jump.
Unity uses C# as its primary scripting language.
C# (pronounced C-sharp) is the language we'll use to write these instructions. Before we can start writing, we need to make sure Unity is set up to work with a code editor.
Setting Up Your Workspace
Unity doesn't have a built-in code editor. Instead, it works with an external program called an Integrated Development Environment, or IDE. The most common one for Unity is Visual Studio. If you installed Unity through the Unity Hub, you were likely prompted to install Visual Studio Community, which is free.
To check if Unity knows where to find your code editor, go to the top menu and select Edit > Preferences. In the window that pops up, click on the External Tools tab. Make sure that Visual Studio is selected as your External Script Editor.
Creating and Attaching Scripts
Let's create our first script. In the Project window at the bottom of the editor, find a good place for your scripts (creating a new "Scripts" folder is a great habit). Right-click inside the folder and go to Create > C# Script.
You'll be prompted to name your new script. This step is more important than it looks.
Name your script immediately, before you do anything else. The name you give the file must match the class name inside the script. Renaming it later can cause errors that are tricky for beginners to fix.
Let’s name it PlayerMovement. Now, how do we give this script to an object?
First, create a simple object to control. Go to GameObject > 3D Object > Cube in the top menu. Now, select your PlayerMovement script in the Project window and drag it onto the Cube in either the Hierarchy window or directly onto the Cube in the Inspector window when the Cube is selected. You should now see the PlayerMovement (Script) component added to your Cube in the Inspector.
Anatomy of a Unity Script
Double-click your PlayerMovement script to open it in Visual Studio. You'll see some code that Unity generated for you automatically. It looks like this:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
}
Let's break this down. The using lines at the top are like importing toolkits. using UnityEngine; is the most important one for us, as it gives us access to all of Unity's game development functions.
The line public class PlayerMovement : MonoBehaviour defines the script itself. The name PlayerMovement must match your file name exactly. The : MonoBehaviour part is the magic that allows this script to be a component you can attach to GameObjects. It's what plugs your code into the Unity engine.
Start and Update
Inside the class, you'll see two special functions, called methods, that Unity created: Start() and Update(). These are fundamental to how Unity scripts work because Unity calls them automatically at specific times.
Start()
verb
A method that Unity calls only once, right before the very first frame of the game when the script is enabled. It's perfect for setup tasks.
Anything you want to happen one time at the very beginning of your object's life goes inside the curly braces {} of the Start method. For example, you could print a message to the console to confirm your script is working.
void Start()
{
// This message will appear in the Unity Console when you press Play.
Debug.Log("Player script has started!");
}
Update()
verb
A method that Unity calls once every single frame. This is where most of your game's logic will go, like checking for player input or moving objects.
Since Update() runs continuously, it's the engine of your game. If your game runs at 60 frames per second, the code inside Update() will run 60 times every second. This is how games create the illusion of smooth, continuous motion and respond instantly to your commands.
For now, just knowing that Start() is for setup and Update() is for ongoing actions is enough to get you started.
Ready to test what you've learned about getting started with scripts?
What is the primary role of a C# script when attached to a GameObject in Unity?
In a newly created Unity C# script, what is the purpose of the Update() method?
You've taken your first step into scripting. You now know how to create a script, attach it to an object, and where to put code for setup versus continuous action. Next, we'll start writing code that actually does something.
