Add points to a lifetime score, once.

Hi, I am making a game with multiple levels, and at the the end of each level, you have a total amount of money (score), I would like to display the lifetime total of of that money on the main menu. However when I enter a new level it is displayed as $0 for that level, and then that score at the end added to the total as well.

Here are the two codes I am using:

Score:

using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using UnityEngine;
using UnityEngine.UI;

public class Score : MonoBehaviour
{

    public static int levelMoney = 0;
    public static int totalLevelMoney = 0;
    public TotalScore totalScore;
    public GameManager gameManager;

    Text money;

    // Start is called before the first frame update
    void Start()
    {
        //Find Money UI
        if (GameObject.Find("Money") != null)
        {
            money = GameObject.Find("Money").GetComponent<Text>();
        }

        //Find TotalMoney UI
        if (GameObject.Find("TotalMoney") != null)
        {
            totalScore = GameObject.Find("TotalMoney").GetComponent<TotalScore>();
        }

        //Find GameManager
        gameManager = GameObject.Find("Game Manager").GetComponent<GameManager>();

        //Reset Score to 0 at beginning of level
        levelMoney = 0;

    }

    // Update is called once per frame
    void Update()
    {
        money.text = "$" + levelMoney;

        if (gameManager.GameOver == true)
        {
            totalLevelMoney = totalLevelMoney + levelMoney;
        }
    }
}

Total Score:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class TotalScore : MonoBehaviour
{
    public static int totalScore = 0; 
    Text totalMoney;

    // Start is called before the first frame update
    void Start()
    {
        //totalScore = totalScore + 1;
        totalMoney = GameObject.Find("TotalMoney").GetComponent<Text>();
    }

    // Update is called once per frame
    void Update()
    {
        totalMoney.text = "$" + (totalScore + Score.levelMoney);
    }

    public void PayOut()
    {
        
    }
}

Everything is working fine, after being a level and getting say $7, it displays $7 on the main menu, however going back into a level and getting say $3, it now says $3 on the menu. I know this is due to the line levelMoney = 0;in the first script.

How can I add the level money to the total money once, and have the total remember that to be able to add more later?

You are going to have to incorporate data persistence of some sort. The way you choose is up to you.


An easy built in option if you’re not worried about people changing the value manually on their devices, you can use the PlayerPrefs and just store that “int” in there and nowhere else. It will persist throughout each level.


A more recommendable and secure option is to use a binary formatter and save things like this deeper in the file system (at a persistent data path). This way the data is saved in binary and is not so easily accessible in the event that someone wanted to cheat.


If this is something that is just going to need to persist during each time the game is open and not necessarily between sessions, then you can use the do not destroy on load function to make the game object persist between scenes.

Cheers