Unity – Scripts 101, an introduction for absolute begginers

0.- Introduction #

If you’ve just opened Unity and keep hearing people say “write a script for that,” this post is for you. We’ll cover what scripts are, when you need them, how to create one, and the smallest bits of C# you need to start making things happen.

0.1.- What is a “script” in Unity? #

In Unity, a script is a C# file that contains code. Most Unity scripts are MonoBehaviours—components you attach to GameObjects in your scene.

Think of it like this:

  • A GameObject is a thing in your scene (player, cube, camera, light).
  • A Component is a feature you add to that thing (Transform, Rigidbody, Collider…).
  • A Script is a component you write yourself to make custom behavior (move, jump, score, interact…).

Unity is component-based: instead of one giant “player” class, you often build behavior by stacking components.

INFO

You can think of a GameObject like a blank phone. Components are apps you install. Your script is a custom app.

0.2.- When do you need scripts? #

You need scripts when you want to:

  • Move or rotate objects
  • Read player input (keyboard/gamepad)
  • Detect collisions and triggers
  • Spawn/despawn objects
  • Track score, health, ammo
  • Build menus and UI logic
  • Control animations and sounds

Unity has lots of built-in components, but scripts let you decide how your game behaves.

1.- Creating your first script #

  1. In the Project window, right-click → CreateC# Script
  2. Name it something clear, like PlayerMover
  3. Double-click it to open in your code editor (Visual Studio, Rider, or VS Code)
  4. Unity will generate a basic script for you.

A common default template looks like this:

🔷
using UnityEngine;

public class PlayerMover : MonoBehaviour
{
    void Start()
    {
        // Runs once, when the object becomes active
    }


    void Update()
    {
        // Runs every frame
    }
}

1.1.- The two methods you’ll see everywhere: Start and Update #

1.1.2.- Start() #

  • Runs once at the beginning (when the GameObject is enabled)
  • Great for setup: get references, initialize variables, set starting values

1.1.3.- Update() #

  • Runs every frame (60 times per second-ish)
  • Great for things that need constant checking: input, movement, timers

If your game is running slowly, it’s often because you’re doing too much work in Update().

1.2.- Attaching a script to a GameObject #

A script won’t do anything until it’s attached.

  1. Select a GameObject in the Hierarchy
  2. Click the “Add Component” button on the bottom of the inspector
  3. Search for your script and click on it

Now that GameObject has your script as a component.

INFO

If you press Play and see errors in the Console, Unity might not run your script.

2.- Let’s move a square #

Create a square in your scene (Right click on the hierarchy > 2D Object > Sprites > Square), attach this script to it, and press Play.

🔷
using UnityEngine;
using UnityEngine.InputSystem;


public class PlayerMover : MonoBehaviour
{
    public float speed = 5f;


    void Update()
    {
        // Get input from WASD keys
        Vector3 direction = new Vector3(0, 0, 0);
        // If the A key is pressed, move left
        if (Keyboard.current.aKey.isPressed)
        {
            // The x axis is negative when moving left
            direction.x = -1;
        }
        // If the D key is pressed, move right
        if (Keyboard.current.dKey.isPressed)
        {
            // The x axis is positive when moving right
            direction.x = 1;
        }
        // If the S key is pressed, move down
        if (Keyboard.current.sKey.isPressed)
        {
            // The y axis is negative when moving down
            direction.y = -1f;
        }
        // If the W key is pressed, move up
        if (Keyboard.current.wKey.isPressed)
        {
            // The y axis is positive when moving up
            direction.y = 1f;
        }

        // To move a position, we add to its current position the direction
        // To control the speed of movement, we multiply the direction by a speed variable
        // To make the movement smooth and frame rate independent, we multiply by Time.deltaTime
        transform.position += direction * speed * Time.deltaTime;
    }
}

INFO

This version was made with the new input system that comes pre-installed on all new projects. Click here to see the same script with the old input system.

Code written with the old input system
🔷
using UnityEngine;
using UnityEngine.InputSystem;


public class PlayerMover : MonoBehaviour
{
    public float speed = 5f;


    void Update()
    {
        // Get input from WASD keys
        Vector3 direction = new Vector3(0, 0, 0);
        direction.x = Input.GetAxis("Horizontal"); // A/D or Left/Right arrows
        direction.y = Input.GetAxis("Vertical");   // W/S or Up/Down arrows

        // To move a position, we add to its current position the direction
        // To control the speed of movement, we multiply the direction by a speed variable
        // To make the movement smooth and frame rate independent, we multiply by Time.deltaTime
        transform.position += direction * speed * Time.deltaTime;
    }
}

2.1.- What is happening here? #

  • using UnityEngine.InputSystem;
    • Enables the New Input System API (like Keyboard.current).
  • Keyboard.current.wKey.isPressed
    • Returns true while the key is held.
  • transform.position += direction * speed * Time.deltaTime
    • Moves the object
  • Time.deltaTime
    • Keeps movement consistent across different frame rates.

2.2.- Mini challenge #

Modify the movement script so:

  1. Holding Shift makes the player move faster.
  2. Pressing Space prints “Jump!” to the Console.
Solution
🔷
using UnityEngine;
using UnityEngine.InputSystem;


public class PlayerMover : MonoBehaviour
{
    public float speed = 5f;


    void Update()
    {
        // Get input from WASD keys
        Vector3 direction = new Vector3(0, 0, 0);
        // If the A key is pressed, move left
        if (Keyboard.current.aKey.isPressed)
        {
            // The x axis is negative when moving left
            direction.x = -1;
        }
        // If the D key is pressed, move right
        if (Keyboard.current.dKey.isPressed)
        {
            // The x axis is positive when moving right
            direction.x = 1;
        }
        // If the S key is pressed, move down
        if (Keyboard.current.sKey.isPressed)
        {
            // The y axis is negative when moving down
            direction.y = -1f;
        }
        // If the W key is pressed, move up
        if (Keyboard.current.wKey.isPressed)
        {
            // The y axis is positive when moving up
            direction.y = 1f;
        }
        // If the left shift key is pressed
        if(Keyboard.current.leftShiftKey.isPressed)
        {
            // Multiplying a direction by a bigger number than 1 makes it longer wich at the end makes the object move faster
            direction *= 2f;
        }

        // To move a position, we add to its current position the direction
        // To control the speed of movement, we multiply the direction by a speed variable
        // To make the movement smooth and frame rate independent, we multiply by Time.deltaTime
        transform.position += direction * speed * Time.deltaTime;
        
        if(Keyboard.current.space.isPressed)
        {
            Debug.Log("Jump!");
        }
    }
}

3.- How Scripts work #

3.1.- Understanding Variables #

Variables store data. You’ll use them constantly. You can imagine them as drawers where you can save things and retreive them later.

Examples:

🔷
int lives = 3; // whole number
float speed = 5.5f; // decimal
bool isAlive = true; // true/false
char tier = 'S'; // Single character
string playerName = "Alex"; // Text and multiple characters
Vector3 position = new Vector3(0, 0, 0); // Stores a position or a direction in 3D space (X,Y,Z)
GameObject player; // Stores another object

3.1.1.- How to use the Inspector #

When a script is attached to an object, Unity shows its public fields in the Inspector.

Why it’s useful:

  • You can change values without editing code
  • You can drag-and-drop references (like a target, sound, prefab

Example:

🔷
public Transform target;

Then in Unity you can drag any object’s Transform into that slot.

3.2.- Access other objects #

There are two beginner-friendly ways:

3.2.1.- Drag and drop from the inspector #

🔷
public GameObject enemy;

Attach the script, then drag the enemy GameObject into the field.

3.2.2.- Find the object through code #

🔷
GameObject player = GameObject.Find("Player");

This works, but it can be slow and breaks if you rename objects. Prefer drag-and-drop when possible.

3.3.- Print messages to the console #

Use Debug.Log() to see what your code is doing.

🔷
Debug.Log("Hello from my script!");
Vector3 direction = new Vector3(1, 3, -2);
Debug.Log(direction);

4.- What to learn next? #

Once you’re comfortable with scripts, the next steps that unlock a lot of power are:

  • Functions / Methods (making your own reusable actions)
  • if statements (decision making)
  • Collisions & Triggers (OnCollisionEnter, OnTriggerEnter)
  • Coroutines (timers, delays, sequences)
  • Prefabs (spawning objects)
  • ScriptableObjects (data-driven design)

4.2.- 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.

4.2.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).