Menu
CoddyTech

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 icon

Challenge

Medium

Bank the coins you dig; only the bank is saved.

  1. In Update(), Space adds 1 to pocket.
  2. In Start(), load bank from the key Bank, 10 if missing, and connect bankButton to a Deposit() that moves the pocket into the bank and saves it.
  3. After every change, show Pocket: 3 and Bank: 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()
    {
    }
}
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Building Real Games

Practice on your own: Unity playground