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?