Recap: Coins in the Bank
Part of the Building Real Games section of Coddy's Unity journey. Lesson 26 of 60.
This chapter saved numbers with PlayerPrefs, gave a new player a default and kept a best only when it was beaten. This treasure hunt puts the saving and the default together.
Challenge
MediumBank the coins you dig; only the bank is saved.
- In
Update(), Space adds 1 topocket. - In
Start(), loadbankfrom the keyBank, 10 if missing, and connectbankButtonto aDeposit()that moves the pocket into the bank and saves it. - After every change, show
Pocket: 3andBank: 12.
Press Play, dig, bank and play again: the bank is kept. 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()
{
}
}
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