Skip to main content

๐Ÿ’ป Lesson 3.1: Your First Script โ€” MonoBehaviour

Time to write code! Don't worry โ€” we go one tiny step at a time. A script is just another component, one whose behaviour you define. Let's make your first one and see it run.

๐ŸŽฏ Learning Objectives

  • Create a C# script and attach it to a GameObject
  • Understand what a MonoBehaviour is
  • Use the two most important methods: Start() and Update()
  • Print your first message to the Console

Estimated Time: 40 minutes

In This Lesson

A Script Is a Component

Remember the component model? A script is simply a component you write yourself. Attach it to a GameObject and it can move that object, respond to input, keep score โ€” anything. Unity uses the language C# (say "C-sharp").

๐Ÿ’ก You don't need prior programming. We'll explain every keyword the first time it appears. Type the examples yourself โ€” that's how it sticks.

Creating a Script

  1. Select a GameObject (say your Cube).
  2. In the Inspector, click Add Component โ–ธ New Script.
  3. Name it Hello and click Create and Add. Unity makes a Hello.cs file and attaches it.
  4. Double-click the script to open it in your code editor (Visual Studio / VS Code).

โš ๏ธ Naming rule

The file name and the class name inside must match exactly. If the file is Hello.cs, the class must be Hello. Renaming one without the other breaks the script.

Anatomy of the Script

A fresh Unity script looks like this. Let's read it line by line:

using UnityEngine;                 // gives us access to Unity's features

public class Hello : MonoBehaviour  // our component, based on MonoBehaviour
{
    // Start is called once, before the first frame
    void Start()
    {
        Debug.Log("Hello, Unity!"); // print a message to the Console
    }

    // Update is called once per frame, over and over
    void Update()
    {

    }
}
  • using UnityEngine; โ€” unlocks Unity's toolbox (like Debug, Transform).
  • public class Hello : MonoBehaviour โ€” defines our component. The : MonoBehaviour part is what lets it attach to a GameObject.
  • Start() and Update() โ€” special methods Unity calls automatically (more below).
  • Debug.Log("...") โ€” writes text to the Console.
  • Every statement ends with a semicolon ; and code lives inside { curly braces }.

๐Ÿ“– Definition

MonoBehaviour: the base class every Unity script builds on. Inheriting from it lets your class be attached to a GameObject as a component and receive Unity's automatic method calls like Start and Update.

Start vs Update โ€” the Two You'll Use Most

Unity calls these for you automatically once the game is running:

MethodWhen it runsUse it forโ€ฆ
Start()Once, at the beginningSetup: initial values, finding things
Update()Every single frame (dozens of times per second)Ongoing work: movement, input, checks
graph TD A[Press Play] --> B[Start called once] B --> C[Update called this frame] C --> D[Update called next frame] D --> E[...every frame until Stop]
๐Ÿ’ก A frame is one drawn image of the game. Games draw many frames per second, so Update() runs many times per second โ€” perfect for smooth movement.

Hands-on Exercise

๐Ÿ‹๏ธ Exercise: Say hello, then count

  1. Create the Hello script above and attach it to any GameObject.
  2. Press Play and open the Console (Window โ–ธ General โ–ธ Console). You should see Hello, Unity! printed once.
  3. Now add a line inside Update() to print every frame:
void Update()
{
    Debug.Log("A new frame just happened!");
}

Press Play again โ€” the message now floods in continuously, one per frame. Press Stop.

๐Ÿ’ก Nothing printed?

Make sure the script is attached to a GameObject that's active in the scene, that you pressed Play, and that the Console window is open. Check the Console for red error text too.

โœ… What this teaches

Start runs once (one hello); Update runs every frame (a flood). That difference is the heartbeat of every Unity game.

๐ŸŽฏ Quick Quiz

Question 1: How often does Update() run?

Question 2: What makes a C# class attachable to a GameObject as a component?

Summary

๐ŸŽ‰ Key Takeaways

  • A script is a component you write in C#.
  • Scripts inherit from MonoBehaviour so they can attach to GameObjects.
  • Start() runs once; Update() runs every frame.
  • Debug.Log() prints to the Console.

๐Ÿš€ What's Next?

Scripts get powerful when they hold data. Lesson 3.2 introduces variables โ€” and the neat trick of editing them right in the Inspector.

๐ŸŽ‰ You wrote code that ran!

That's a genuine milestone. It only gets more fun from here.