- Home /
Currency not getting added between levels
Hello, I want the Currency collected during a level of my game to be added afterwards. But instead this is happening:
1st Run: Player collects 2 Coins during Run -> 2 Coins is being shown in the Main Menu
2nd Run: Player collects 0 coins during Run -> 0 Coins is being shown in the Main Menu
I'm trying to fix this bug since days and rewrote my code several times. I would appreciate any help!
The code for the Main Menu:
public class MainMenuManager : MonoBehaviour {
public int HighScore=0;
public int reachedScore;
public Text HighScoreUI;
public Text CurrencyUI;
public int currency;
// Use this for initialization
void Start () {
HighScore = PlayerPrefs.GetInt("HighScore");
currency = PlayerPrefs.GetInt("Currency");
}
// Update is called once per frame
void Update () {
CurrencyUI.text = " " + currency.ToString("0");
HighScoreUI.text = "High Score: " + HighScore;
PlayerPrefs.SetInt("Currency", currency);
}
}
Code for collecting the currency:
void OnTriggerEnter2D(Collider2D other)
{
if(other.gameObject.tag=="Currency" && playerDead == false)
{
gameManager.currency += 1; //Currency is being added to the GameManager
barking.Play();
Destroy(other.gameObject);
}
}
And the Code for the GameManager:
public class Gamemanager : MonoBehaviour {
public int currency; //being accessed by the GameObject onTriggerEnter
private string activeScene;
private int balance;
void Start () {
gameOverObjects = GameObject.FindGameObjectsWithTag("ShowOnGameOver");
for(int i=0; i<gameOverObjects.Length;i++)
{
gameOverObjects[i].SetActive(false);
}
render = player.GetComponent<SpriteRenderer>();
playeranim = player.GetComponent<Animator>();
defeatMusic = gameObject.GetComponent<AudioSource>();
balance = PlayerPrefs.GetInt("Currency"); //PlayerPrefs is set in MainMenu
activeScene = PlayerPrefs.GetString("Scene");
}
// Update is called once per frame
void Update () {
score += PointsPerSecond * Time.deltaTime;
scoreUI.text = "Score: " + score.ToString("0");
currencyUI.text = ": " + currency.ToString("0");
}
public void restartGame()
{
StartCoroutine("RestartGameCo");
}
public IEnumerator RestartGameCo()
{
yield return new WaitForSeconds(0.5f);
PlayerPrefs.SetInt("Player Score", (int)score);
PlayerPrefs.SetInt("Currency", balance+currency); //Collected currency is added to the balance and saved to the PlayerPref
playeranim.SetBool("gameover", true);
activeScene = PlayerPrefs.GetString("Scene");
if (activeScene == "ben")
{
Application.LoadLevel("gameOver2");
}
if(activeScene == "ben_desert")
{
Application.LoadLevel("gameOverDesert");
}
if(activeScene == "ben_winter")
{
Application.LoadLevel("gameOverWinter");
}
for (int i = 0; i < gameOverObjects.Length; i++)
{
gameOverObjects[i].SetActive(true);
}
}
}
Comment
Your answer
Follow this Question
Related Questions
Problem with Fuse/Mixamo Animations 0 Answers
Child of a player not syncing position with network? 0 Answers
how do i network sync scaling of character? 1 Answer
How to let player know they have completed a level 2 Answers
Enemy wont damage player 2 Answers