Unity Lesson: Create a Simple Collect-the-Coin Game
Beginner 2D Unity Lesson
Estimated Time: 45–60 minutes
Lesson Goal: Students will create a basic 2D game where a player moves around the screen and collects coins.
Learning Objectives
- Create a new Unity 2D project.
- Identify the Scene, Game, Hierarchy, Inspector, Project, and Console windows.
- Add GameObjects to a scene.
- Add a Rigidbody2D and Collider2D.
- Write a simple C# movement script.
- Detect when the player touches a collectible.
- Run and test a Unity game.
Part 1 — Create the Project
- Open Unity Hub.
- Click New Project.
- Select a 2D project template.
- Name the project:
PX_lastname_CoinCollector
- Save your project on the Google Drive under
2026_27KleinCollins\yourclass.
- Click Create Project.
Part 2 — Create the Player
- In the Hierarchy window, right-click.
- Select 2D Object → Sprites → Square.
- Rename the object: Player.
- Select the Player.
- In the Inspector, change its scale if necessary so it is easy to see.
- Click Add Component and add Rigidbody2D.
- Add Box Collider 2D.
- Under Rigidbody2D settings, set Gravity Scale = 0. This prevents the player from falling.
Part 3 — Create the Player Movement Script
- In the Project window, right-click.
- Select Create → C# Script.
- Name it:
PlayerMovement.
- Double-click the script.
- Replace the code with the following:
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
public float speed = 5f;
void Update()
{
float horizontal = Input.GetAxis("Horizontal");
float vertical = Input.GetAxis("Vertical");
Vector3 movement =
new Vector3(horizontal, vertical, 0);
transform.position +=
movement * speed * Time.deltaTime;
}
}
- Save the script.
- Return to Unity and drag the
PlayerMovement script onto the Player GameObject.
Part 4 — Test the Player
- Click the Play button.
- Use W or Up Arrow to move up.
- Use S or Down Arrow to move down.
- Use A or Left Arrow to move left.
- Use D or Right Arrow to move right.
- The square should move around the screen.
- Click Play again to stop the game.
Part 5 — Create a Coin
- In the Hierarchy, right-click.
- Select 2D Object → Sprites → Circle.
- Rename it: Coin.
- Resize it so it is smaller than the Player.
- Add a Circle Collider 2D component.
- In the Circle Collider 2D component, check Is Trigger. This allows Unity to detect the player touching the coin without creating a physical collision.
Part 6 — Create the Coin Collection Script
- Create another C# script named
Coin.
- Enter the following code:
using UnityEngine;
public class Coin : MonoBehaviour
{
private void OnTriggerEnter2D(Collider2D other)
{
if (other.gameObject.name == "Player")
{
Debug.Log("Coin Collected!");
Destroy(gameObject);
}
}
}
- Attach the script to the Coin.
Part 7 — Test the Game
- Click Play.
- Move the Player into the Coin.
- When the player touches the coin, the coin should disappear.
- The Console should display:
Coin Collected!
Part 8 — Add More Coins
- Stop the game.
- Select the Coin in the Hierarchy and duplicate it several times using Ctrl + D.
- Move the coins to different locations around the screen.
- Run the game again and collect all the coins.
Challenge Activities
- Change the player’s movement speed.
- Change the size of the player.
- Add 10 coins.
- Create obstacles.
- Add walls around the game area.
- Create a score variable.
- Display the score on the screen.
- Add a sound when a coin is collected.
- Add a timer.
- Create a You Win! message after all coins are collected.
Key Unity Concepts
| Unity Concept |
Purpose |
| GameObject | An object that exists in the game |
| Transform | Controls position, rotation, and scale |
| Sprite | A 2D image used in the game |
| Rigidbody2D | Adds 2D physics behavior |
| Collider2D | Defines an object’s collision area |
| Script | C# code controlling game behavior |
| Inspector | Changes properties of selected objects |
| Hierarchy | Lists objects in the current scene |
| Scene | Where the developer builds the game |
| Game | Shows what the player sees |
| Console | Displays messages and programming errors |