A
A
ANYJT2022-04-20 14:51:37
C++ / C#
ANYJT, 2022-04-20 14:51:37

Why is the data not loading?

I made a save, I check it, but it does not load / save the data.
Here is the code, please help.

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

public class Clicker : MonoBehaviour

{
    public int money;
    public GameObject b1;
    public GameObject b2;
    public GameObject b3;
    public GameObject b4;
    public int bonusplus = 2;
    public int bonusplus2 = 7;
    public Text moneyText;
    public int bonus = 1;
    public int autoclickbonus2 = 0;
    public void OnApplicationQuit()
    {
        PlayerPrefs.GetInt("money", money);
    }
    private void Update()
    {
        moneyText.text = money.ToString();
    }
    public void LoadGame()
    {
        money = PlayerPrefs.GetInt("money");
        Debug.Log("Game data loaded!");
    }
    private void Start()
    {
        moneyText.text = money.ToString();
        LoadGame();
    }
    public void ToShop()
    {
        SceneManager.LoadScene(1);
    }

    public void Return()
    {
        SceneManager.LoadScene(0);
    }
    public void ButtonClick()
    {
        money += bonus;
    }
    public void Buy()
    {
        if (money >= 300)
        {


            bonus += bonusplus;
            money -= 300;
            b1.SetActive(false);
        }
        else
        {
            Debug.Log("Not enough money");
        }
    }
    public void Buy2()
    {
        if (money >= 900)
        {
            b2.SetActive(false);
            bonus += bonusplus2;
            money -= 900;
        }
        else
        {
            Debug.Log("Not enough money");
        }
    }
    public void Buy3()
    {
        if (money >= 3000)
        {
            b3.SetActive(false);
            StartCoroutine(IdleFarm());
            money -= 3000;
        }
        else
        {
            Debug.Log("Not enough money");
        }
    }
    public void Buy4()
    {
        if (money >= 4000)
        {
            b4.SetActive(false);
            autoclickbonus2 += 4;
            money -= 4000;
        }
        else
        {
            Debug.Log("Not enough money");
        }
    }
    IEnumerator IdleFarm()
    {
        yield return new WaitForSeconds(1);
        money += autoclickbonus2;
        Debug.Log(money);
        StartCoroutine(IdleFarm());
    }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
KraGen.Developer, 2022-04-20
@KraGenDeveloper

Look, you're declaring that "money" takes its value from Playerprefs. In order for the money to change after the purchase, you need to enter a new value PlayerPrefs.SetInt("money");
Here is an example

int mon = PlayerPrefs.GetInt("Money");
                if(mon >= 200){
                    mon -= 200;
                    PlayerPrefs.SetInt("Money",mon);
                }

So that everything is preserved yuzayPlayerPrefs.Save();

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question