Gems on Screen
Part of the Building Real Games section of Coddy's Unity journey. Lesson 51 of 56.
Challenge
MediumGems 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_TextfieldgemTextthat 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, asGems: 0/5. - A public method
AddGem()adds one gem and shows the new count, asGems: 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
2Buttons and Screens
Buttons and onClickLambdasWiring in the InspectorPanels On and OffHealth BarsRecap: Upgrade Shop5Saving with PlayerPrefs
Save a NumberLoad with a DefaultHigh ScoreSave SettingsRecap: Coins in the Bank3The Game Manager
One Script Runs the GameThe SingletonGame StatesSwitching on the StateRecap: Round Manager6Project: Endless Runner
Three LanesObstacles Ahead9The New Input System
Two Input SystemsKeyboard and MousePressed This FrameInput ActionsRecap: Input Swap