Recap: Upgrade Shop
Part of the Building Real Games section of Coddy's Unity journey. Lesson 11 of 60.
This chapter wired buttons to code, kept the screen in step with the numbers, and turned buttons on and off. The shop puts them together.
Challenge
EasyBuild the mine's shop.
- In
Start(), hook the buttons: Mine addsperClicktocoins; the pick (pickCost) adds 1 toperClickand costs 10 more; the lantern (lanternCost) adds 3. - Write
Refresh(), called inStart()and after each click:Coins: 12,Pick: 20,Lantern: 25orLantern: owned. Only affordable, unowned upgrades are interactable.
Press Play and 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;
private int coins;
private int perClick = 1;
private bool hasLantern;
// Hook the three buttons in Start(), and show everything in Refresh()
}
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
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 SwapPractice on your own: Unity playground