Recap: Upgrade Shop
Part of the Building Real Games section of Coddy's Unity journey. Lesson 11 of 56.
Challenge
EasyA gold mine you dig by clicking. Write GoldMine.cs (the scene links the three buttons and the three texts):
- Each click on Mine earns coins, 1 per click at first.
coinsTextshows them, asCoins: 12. - The pick costs
pickCost(10). Buying it spends the coins, adds 1 to the coins per click and raises its price by 10. Its button's text,pickText, shows the price, asPick: 20. - The lantern costs
lanternCost(25) and can be bought once: it adds 3 to the coins per click, and from then onlanternTextreadsLantern: owned. Before that it readsLantern: 25. - An upgrade's button is interactable only while you have enough coins for it, and the lantern's never again once it is owned.
- All three texts and both buttons are right from the start.
Press Play, dig, buy both upgrades, then Check.
Try it yourself
using UnityEngine;
using UnityEngine.UI;
using TMPro;
public class GoldMine : MonoBehaviour
{
public Button mineButton;
public Button pickButton;
public Button lanternButton;
public TMP_Text coinsText;
public TMP_Text pickText;
public TMP_Text lanternText;
public int pickCost = 10;
public int lanternCost = 25;
// Mine earns coins; the pick and the lantern are upgrades, usable only when you can afford them
}
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