Unity Game Engine • High School • Lesson 5

Your First C# Script

Mission: Make Your GameObject Talk!

Difficulty: Very Easy45–50 minutesC# • Components • MonoBehaviour • Start()

Lesson Overview

Every video game needs instructions that tell characters and objects what to do. In Unity, programmers write these instructions using C#. Students will create their first C# script, attach it to a GameObject, and display personalized messages in Unity’s Console when the game starts.

1. Learning Objectives

  1. Explain the purpose of C# scripts in Unity.
  2. Create a C# script and open it in a code editor.
  3. Identify MonoBehaviour and Start().
  4. Use Debug.Log() to display Console messages.
  5. Attach a script to a GameObject.
  6. Run, test, and troubleshoot the program.

2. Key Vocabulary

TermDefinition
C#The programming language used to create scripts in Unity.
GameObjectAn object in a Unity scene, such as a cube, character, or camera.
ComponentA feature or behavior attached to a GameObject.
ScriptA file containing programming instructions.
MonoBehaviourA Unity class that lets scripts function as GameObject components.
Start()A Unity method called once when an enabled script first starts running.
Debug.Log()A method that writes a message to the Console.
ConsoleA Unity window showing messages, warnings, and errors.

3. Warm-Up — 5 Minutes

Discussion: How does a video game know what to do when you press Play?

Think about characters that move, jump, collect coins, or display messages. Scripts give GameObjects instructions for these behaviors.

Think About It: A GameObject is like a robot. A C# script gives it instructions. Attaching a script is like installing its program.

4. Guided Activity — 20 Minutes

Step 1 — Open Your Unity Project

  1. Open Unity Hub and your existing Unity project.
  2. Open your previous scene or create a new one.
  3. Save the scene as Lesson5_FirstScript.

Step 2 — Create a GameObject

  1. Right-click in the Hierarchy window.
  2. Select 3D Object → Cube. For a 2D project, use 2D Object → Sprites → Square instead.
  3. Rename the object MessageCube or MessageSquare.
  4. Select it to inspect its components.

Step 3 — Create a C# Script

  1. In the Project window, select Assets.
  2. Create a folder called Scripts if needed.
  3. Right-click the folder and select Create → Scripting → MonoBehaviour Script (wording varies by Unity version).
  4. Name the script FirstMessage.
  5. Double-click it to open it in your C# editor.
Important: The file FirstMessage.cs must have a matching class named FirstMessage.

Step 4 — Write Your First Program

Replace the starter code with the program below:

using UnityEngine;

// Lesson 5: First C# Script
// This script displays a message when the game starts.

public class FirstMessage : MonoBehaviour
{
    // Start runs once when this script begins.
    void Start()
    {
        // Display a message in the Unity Console.
        Debug.Log("Hello, Unity! My first C# script works!");
    }
}

Step 5 — Understand the Code

CodeExplanation
using UnityEngine;Allows access to Unity programming features.
public class FirstMessageDefines the class.
: MonoBehaviourEnables the script to act as a Unity component.
void Start()Defines instructions called once when the script starts.
{ }Groups a set of programming instructions.
Debug.Log()Writes a Console message.
;Ends a C# statement.
//Begins a comment.

Step 6 — Attach the Script

  1. Save the C# file and return to Unity.
  2. Allow the script to compile.
  3. Select MessageCube or MessageSquare in the Hierarchy.
  4. Drag the FirstMessage asset onto the Inspector, or find it using Add Component.
  5. Confirm that the FirstMessage component appears.

Step 7 — Run the Program

  1. Open Window → General → Console.
  2. Click Play at the top of Unity.
  3. Look for the message below.
  4. Click Play again to exit Play mode.
Expected Unity Console Output
Hello, Unity! My first C# script works!

Success! Your first C# script is running.

5. Independent Assignment — 15 Minutes

Mission: Create a Personalized Game Introduction. Modify your script to display three messages: a welcome, the programmer’s name, and the game’s goal.

Example Program

using UnityEngine;

// Programmer: Your Name
// Lesson 5: First C# Script
// Purpose: Display a game introduction.

public class FirstMessage : MonoBehaviour
{
    void Start()
    {
        Debug.Log("Welcome to Treasure Quest!");
        Debug.Log("Programmed by: Your Name");
        Debug.Log("Mission: Collect 10 coins to win!");
    }
}

Student Checklist

0 of 8 complete

6. Challenge Activity — 5 Minutes

Add a warning and an example error message:

Debug.LogWarning("Warning: Watch out for enemies!");
Debug.LogError("Example error: Player health is zero!");

These are practice messages, not actual gameplay errors. Discuss how developers use logs, warnings, and errors to troubleshoot games.

7. Troubleshooting

ProblemPossible Fix
No message appearsConfirm the script is attached, the GameObject and component are enabled, and Play mode is active.
Script cannot be attachedCheck the filename/class-name match and resolve compilation errors.
Red compiler errorInspect brackets, quotation marks, spelling, and semicolons.
Console is emptyOpen Console and ensure the message filter is enabled.
Script will not openCheck Unity's external script editor preferences.

8. Knowledge Check — Interactive Quiz

Choose an answer for each question, then select Check Answers.

1. What programming language are Unity scripts typically written in?
2. What does MonoBehaviour allow a script to do?
3. When does Start() normally execute?
4. Which command writes a message to the Console?
5. Where should students look to see Debug.Log messages?

9. Assessment Rubric — 100 Points

CriteriaPoints
GameObject created and appropriately named10
C# script created with correct class name15
MonoBehaviour and Start() used correctly20
Three personalized Console messages20
Three meaningful code comments10
Script attached and runs successfully15
Scene saved and evidence submitted10
Total100

Submission Instructions

Submit one screenshot showing the GameObject selected in the Hierarchy, the attached script in the Inspector, and all three messages in the Console. Also submit your FirstMessage.cs file and any project files required by the teacher.

10. Exit Ticket — 5 Minutes

  1. Why must a script be attached to a GameObject?
  2. What is the difference between Start() and Debug.Log()?
  3. How could you use a similar script in a real video game?

Teacher Notes

This lesson follows beginner Unity lessons on scenes, sprites, and GameObjects. Demonstrate the script first, guide students through setup, and then have them personalize their own programs.

Teaching Point: Debug.Log() displays text in Unity’s Console—not on the player's game screen. Player-facing text with a Canvas or TextMeshPro can be taught in a future lesson.

Next lesson: Lesson 6 — C# Variables and the Inspector.

Reference: Unity Learn: Awake and Start · Unity MonoBehaviour.Start documentation