So you’ve opened Unity and heard that you need a “script.” What is that? In short: a script is a small set of instructions you write in C# that tells a GameObject what to do.
This page walks you, step by step, through what a script is, how to make one, and how to read and tweak simple code even if you’ve never programmed before. We’ll go slowly, show practical examples, and give you copy‑paste snippets you can modify.
0.- Basic Concepts #
0.1.- Quick Vocabulary #
- GameObject: any thing in your scene (a cube, a light, your player, a door…). Think of it as a container.
- Component: a behavior or property you add to a GameObject (Transform, Rigidbody, AudioSource). Think of it as a feature.
- Script (C#): your own custom component. It lets you decide what happens (move, jump, open doors…).
- Inspector: the panel where you see and edit component values.
- MonoBehaviour: the basic Unity class your scripts inherit from so Unity can run them.
- Frame: a single screen update. Unity runs your code many times per second.
- Local vs World: local means relative to the object’s own rotation/position; world means the scene’s global coordinates.
INFO
You can think of a GameObject like a blank phone. Components are apps you install. Your script is a custom app.
0.1.1.- Quick Programming Vocabulary #
- Code: the text you write to tell the computer what to do.
- Script: a small program attached to something in Unity.
- Function: a group of code lines that can be “called” to execute them.
- Variable: a named box that stores a value. It’s like a drawer where you store things.
- Type: the kind of value (number, text, true/false, etc.).
- int: Stores an integer number (a number with no decimals)
- float: Stores a number with decimals
- bool: Stores true/false
- string: Stores text
- Vector3: Stores 3 floats (x,y,z) and has some special functions to operate it. We can use a Vector3 to represent a direction, a position, a rotation…
- Quaternion: Used exclusively to represent a rotation in 3D. We will avoid this and use more common methods when possible.
- Return value: what a function gives back after it runs.
- If (condition): only do something when a test is true.
- Loop: repeat something multiple times (you’ll see this later).
- Bug / Error: when the code doesn’t work as expected.
- Null: “nothing here”, when a variable is empty, a missing reference.
- Compile / Build: turn your code into something the computer or a device can run.
- Debug: find and fix problems (often with
Debug.Log
).
0.2.- What a Unity script actually is #
A Unity script is a C# file (ends in .cs
) that defines a class. When this class inherits from MonoBehaviour
, Unity can attach it to GameObjects as a component and automatically call special functions (like Start()
or Update()
).
0.2.1.- The life cycle #
Unity looks for certain function names and calls them at the right times:
Start()
: Runs once right before the first frame when the GameObject appears on the scene. Use to set initial values that depend on other objects being ready.Update()
: runs every frame. Use for input, non‑physics movement, timers.FixedUpdate()
: runs once per physics update. Use for physics (Rigidbody
).
Exercise 1 – Make your first script #
Goal: move a cube with WASD/arrow keys.
- Create something to move
- Top menu: GameObject → 3D Object → Cube.
- Rename it to Player.
- Create a script
- In the Project window: Right‑click → Create → C# Script.
- Name it Mover (exactly this—file name must match the class name).
- Attach the script
- Drag Mover.cs onto the Player in the Hierarchy (or click Add Component in the Inspector and search “Mover”).
- Open the script
- Double‑click Mover.cs to open it in your code editor.
- Paste this code (replace everything in the file):
using UnityEngine;
public class Mover : MonoBehaviour
{
// Shown in the Inspector. You can tune it without editing code.
public float speed = 3f;
void Update()
{
// Read keyboard input (WASD/Arrows)
float h = Input.GetAxis("Horizontal");
float v = Input.GetAxis("Vertical");
// Build a direction vector from input
Vector3 direction = new Vector3(h, v);
// Move in world space, framerate‑independent
transform.position = transform.position + direction * speed * Time.deltaTime;
}
}
- Press Play
- Select the Player and adjust Speed in the Inspector while the game runs. Stop Play and set a value you like.
That’s it, your first unity script.