Menu
CoddyTech

Gems on Screen

Part of the Building Real Games section of Coddy's Unity journey. Lesson 51 of 56.

challenge icon

Challenge

Medium

Gems float over the course: five turquoise cubes tagged Gem, each with a trigger and the Gem script. The HUD canvas shows GemText at the top left, and the scene puts it in the Player's new gemText field. Keep everything from the earlier lessons.

In Player (add using TMPro;):

  • A TMP_Text field gemText that the Inspector sets, the gems collected, and the level's total.
  • In Start, count the level's gems by their tag and show the count, as Gems: 0/5.
  • A public method AddGem() adds one gem and shows the new count, as Gems: 1/5.

In Gem: when the player (an object with the Player script) enters, call its AddGem() and remove the gem.

Press Play and collect a few gems, then Check.

Try it yourself

using UnityEngine;

public class Gem : MonoBehaviour
{
    // A later lesson fills this in: a gem to collect.
}

All lessons in Building Real Games