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

Part 1 — Create the Project

  1. Open Unity Hub.
  2. Click New Project.
  3. Select a 2D project template.
  4. Name the project: PX_lastname_CoinCollector
  5. Save your project on the Google Drive under 2026_27KleinCollins\yourclass.
  6. Click Create Project.

Part 2 — Create the Player

  1. In the Hierarchy window, right-click.
  2. Select 2D Object → Sprites → Square.
  3. Rename the object: Player.
  4. Select the Player.
  5. In the Inspector, change its scale if necessary so it is easy to see.
  6. Click Add Component and add Rigidbody2D.
  7. Add Box Collider 2D.
  8. Under Rigidbody2D settings, set Gravity Scale = 0. This prevents the player from falling.

Part 3 — Create the Player Movement Script

  1. In the Project window, right-click.
  2. Select Create → C# Script.
  3. Name it: PlayerMovement.
  4. Double-click the script.
  5. 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;
    }
}
  1. Save the script.
  2. Return to Unity and drag the PlayerMovement script onto the Player GameObject.

Part 4 — Test the Player

  1. Click the Play button.
  2. Use W or Up Arrow to move up.
  3. Use S or Down Arrow to move down.
  4. Use A or Left Arrow to move left.
  5. Use D or Right Arrow to move right.
  6. The square should move around the screen.
  7. Click Play again to stop the game.

Part 5 — Create a Coin

  1. In the Hierarchy, right-click.
  2. Select 2D Object → Sprites → Circle.
  3. Rename it: Coin.
  4. Resize it so it is smaller than the Player.
  5. Add a Circle Collider 2D component.
  6. 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

  1. Create another C# script named Coin.
  2. 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);
        }
    }
}
  1. Attach the script to the Coin.

Part 7 — Test the Game

  1. Click Play.
  2. Move the Player into the Coin.
  3. When the player touches the coin, the coin should disappear.
  4. The Console should display: Coin Collected!

Part 8 — Add More Coins

  1. Stop the game.
  2. Select the Coin in the Hierarchy and duplicate it several times using Ctrl + D.
  3. Move the coins to different locations around the screen.
  4. Run the game again and collect all the coins.

Challenge Activities

Key Unity Concepts

Unity Concept Purpose
GameObjectAn object that exists in the game
TransformControls position, rotation, and scale
SpriteA 2D image used in the game
Rigidbody2DAdds 2D physics behavior
Collider2DDefines an object’s collision area
ScriptC# code controlling game behavior
InspectorChanges properties of selected objects
HierarchyLists objects in the current scene
SceneWhere the developer builds the game
GameShows what the player sees
ConsoleDisplays messages and programming errors