Recap: Coins in the Bank
Part of the Building Real Games section of Coddy's Unity journey. Lesson 26 of 56.
Challenge
MediumA treasure hunt. Space digs up a coin into the pocket, and the Bank button moves the pocket into the bank. Only the bank is saved: coins still in the pocket are lost when the game is closed. Write Digger:
- Space adds 1 coin to
pocket. - Clicking Bank adds the pocket to
bank, empties the pocket and saves the bank under the keyBank. - A new player's bank starts with 10 coins, a welcome gift. A bank that was saved, even at 0, loads as it was saved.
- The texts always show the current values, as Pocket: 3 and Bank: 12.
Press Play, dig, bank, and play again, then Check.
Try it yourself
using UnityEngine;
using UnityEngine.UI;
using TMPro;
public class Digger : MonoBehaviour
{
public TMP_Text pocketText;
public TMP_Text bankText;
public Button bankButton;
private int pocket;
private int bank;
void Start()
{
}
void Update()
{
}
}
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