I'm using playerprefs to store the coins collected during the current level and it add it to the previously collected coins. The problem is when i go to the shop menu my coins are getting doubled like i have earned 50 coins during the current level then in the shop the total coins is 100 whereas the total coins should be 50. And when i close the game my total coins in the menu gets replaced with the one's in the shop.
Here's my save system script:-
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using TMPro;
public class SaveSystem : MonoBehaviour
{
public TextMeshProUGUI menu;
int total;
// Start is called before the first frame update
void Start()
{
total += PlayerPrefs.GetInt("coins") + Score.score;
menu.text = " : " + total.ToString();
save();
}
public void nextLevel()
{
save();
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);
Time.timeScale = 1f;
Score.score = 0;
}
public void mainMenu()
{
SceneManager.LoadScene("MainMenu");
Time.timeScale = 1f;
}
public void resetGame()
{
PlayerPrefs.DeleteKey("coins");
SceneManager.LoadScene("MainMenu");
}
public void save()
{
PlayerPrefs.SetInt("coins", total);
PlayerPrefs.Save();
}
}
And my shop script is :-
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro;
public class ShopSystem : MonoBehaviour
{
public GameObject btn2, btn3, btn4, btn5;
public Button skin1, skin2, skin3, skin4, skin5;
bool skinBought2, skinBought3, skinBought4, skinBought5;
//public float errorMessage = 3f;
public TextMeshProUGUI insufficientFunds;
int coins;
public TextMeshProUGUI buyText;
public void Start()
{
coins = PlayerPrefs.GetInt("coins");
skin1.interactable = true;
skin2.interactable = false;
skin3.interactable = false;
skin4.interactable = false;
skin5.interactable = false;
insufficientFunds.enabled = false;
}
void noFunds()
{
insufficientFunds.enabled = false;
}
public void Buy2()
{
if ( coins >= 120)
{
Destroy(btn2);
skin2.interactable = true;
coins -= 120;
buyText.text = coins.ToString();
PlayerPrefs.SetInt("coins", coins);
PlayerPrefs.Save();
}
else
{
insufficientFunds.enabled = true;
Invoke("noFunds", 3f);
}
}
public void Buy3()
{
if(coins >= 220)
{
Destroy(btn3);
skin3.interactable = true;
coins -= 220;
buyText.text = coins.ToString();
PlayerPrefs.SetInt("coins", coins);
PlayerPrefs.Save();
}
else
{
insufficientFunds.enabled = true;
Invoke("noFunds", 3f);
//Debug.Log("Insufficient funds!");
}
}
public void Buy4()
{
if(coins >= 320)
{
Destroy(btn4);
skin4.interactable = true;
coins -= 320;
buyText.text = coins.ToString();
PlayerPrefs.SetInt("coins", coins);
PlayerPrefs.Save();
}
else
{
insufficientFunds.enabled = true;
Invoke("noFunds", 3f);
//Debug.Log("Insufficient Funds");
}
}
public void Buy5()
{
if(coins >= 420)
{
Destroy(btn5);
skin5.interactable = true;
coins -= 420;
buyText.text = coins.ToString();
PlayerPrefs.SetInt("coins", coins);
PlayerPrefs.Save();
}
else
{
insufficientFunds.enabled = true;
Invoke("noFunds", 3f);
//Debug.Log("Insufficient Funds");
}
}
}
↧