Your First C# Script
Mission: Make Your GameObject Talk!
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
- Explain the purpose of C# scripts in Unity.
- Create a C# script and open it in a code editor.
- Identify
MonoBehaviourandStart(). - Use
Debug.Log()to display Console messages. - Attach a script to a GameObject.
- Run, test, and troubleshoot the program.
2. Key Vocabulary
| Term | Definition |
|---|---|
| C# | The programming language used to create scripts in Unity. |
| GameObject | An object in a Unity scene, such as a cube, character, or camera. |
| Component | A feature or behavior attached to a GameObject. |
| Script | A file containing programming instructions. |
| MonoBehaviour | A 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. |
| Console | A 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.
4. Guided Activity — 20 Minutes
Step 1 — Open Your Unity Project
- Open Unity Hub and your existing Unity project.
- Open your previous scene or create a new one.
- Save the scene as
Lesson5_FirstScript.
Step 2 — Create a GameObject
- Right-click in the Hierarchy window.
- Select 3D Object → Cube. For a 2D project, use 2D Object → Sprites → Square instead.
- Rename the object
MessageCubeorMessageSquare. - Select it to inspect its components.
Step 3 — Create a C# Script
- In the Project window, select
Assets. - Create a folder called
Scriptsif needed. - Right-click the folder and select Create → Scripting → MonoBehaviour Script (wording varies by Unity version).
- Name the script
FirstMessage. - Double-click it to open it in your C# editor.
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
| Code | Explanation |
|---|---|
using UnityEngine; | Allows access to Unity programming features. |
public class FirstMessage | Defines the class. |
: MonoBehaviour | Enables 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
- Save the C# file and return to Unity.
- Allow the script to compile.
- Select
MessageCubeorMessageSquarein the Hierarchy. - Drag the
FirstMessageasset onto the Inspector, or find it using Add Component. - Confirm that the FirstMessage component appears.
Step 7 — Run the Program
- Open Window → General → Console.
- Click Play at the top of Unity.
- Look for the message below.
- Click Play again to exit Play mode.
Success! Your first C# script is running.
5. Independent Assignment — 15 Minutes
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
| Problem | Possible Fix |
|---|---|
| No message appears | Confirm the script is attached, the GameObject and component are enabled, and Play mode is active. |
| Script cannot be attached | Check the filename/class-name match and resolve compilation errors. |
| Red compiler error | Inspect brackets, quotation marks, spelling, and semicolons. |
| Console is empty | Open Console and ensure the message filter is enabled. |
| Script will not open | Check Unity's external script editor preferences. |
8. Knowledge Check — Interactive Quiz
Choose an answer for each question, then select Check Answers.
9. Assessment Rubric — 100 Points
| Criteria | Points |
|---|---|
| GameObject created and appropriately named | 10 |
| C# script created with correct class name | 15 |
| MonoBehaviour and Start() used correctly | 20 |
| Three personalized Console messages | 20 |
| Three meaningful code comments | 10 |
| Script attached and runs successfully | 15 |
| Scene saved and evidence submitted | 10 |
| Total | 100 |
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
- Why must a script be attached to a GameObject?
- What is the difference between
Start()andDebug.Log()? - 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.
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