- Home /
Question by
adityarocks123456 · May 05, 2021 at 07:24 PM ·
scripting problemscripting beginnerplayerprefs
Coins not updating after making shop transactions
I have a save system which is saving my total coins using playerprefs. Now in the shop menu I'm calling the playerprefs using getInt and using it to see how many total coins I have. On making a transaction the amount gets deducted from the total coins. However, on going back to the menu my coins are same(i.e. before the transaction) and when restarting the game it works fine. How can I do it in the runtime ?
Here, is my script for save System 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 SYSTEM SCRIPT IS
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro;
using UnityEngine.SceneManagement;
public class Shop : MonoBehaviour
{
public TextMeshProUGUI diamonds;
int reduce;
private void Update()
{
diamonds.text = " : " + PlayerPrefs.GetInt("coins").ToString();
}
public void testBuy()
{
if (PlayerPrefs.GetInt("coins") >= 120)
{
reduce = PlayerPrefs.GetInt("coins") - 120;
PlayerPrefs.SetInt("coins", reduce);
PlayerPrefs.Save();
Debug.Log("Purchase Complete");
}
else
{
Debug.Log("Get Funds !!");
}
}
public void Back()
{
}
}
Comment