๐ป 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
- Select a GameObject (say your Cube).
- In the Inspector, click Add Component โธ New Script.
- Name it
Helloand click Create and Add. Unity makes aHello.csfile and attaches it. - 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 (likeDebug,Transform).public class Hello : MonoBehaviourโ defines our component. The: MonoBehaviourpart is what lets it attach to a GameObject.Start()andUpdate()โ 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:
| Method | When it runs | Use it forโฆ |
|---|---|---|
| Start() | Once, at the beginning | Setup: initial values, finding things |
| Update() | Every single frame (dozens of times per second) | Ongoing work: movement, input, checks |
๐ก 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
- Create the
Helloscript above and attach it to any GameObject. - Press Play and open the Console (Window โธ General โธ Console). You should see
Hello, Unity!printed once. - 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.