Answer the question
In order to leave comments, you need to log in
Why is money not deducted when buying from a variable?
I am making a 2D game on Unity 2020.3.30f1 for android. I made a store, now the money is not taken away when buying, but remains unchanged, I don’t know how to do something when buying, the money is deducted from the variable data.money (script 2). What should I do? Thanks in advance!
Script 1: (He keeps the money)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class ShopAssist : MonoBehaviour
{
public static int money;
public Text moneyText;
public bool isMulti = false;
void Start()
{
money = PlayerPrefs.GetInt("Money");
}
void Update()
{
moneyText.text = money.ToString();
}
public void BuyMulti()
{
if (money >= 10 && isMulti == false)
{
isMulti = true;
money -= 10;
PlayerPrefs.SetInt("Money", money);
PlayerPrefs.SetInt("isMulti", isMulti ? 1 : 0);
}
}
public bool TryRemoveMoney(int moneyToRemove)
{
if (money >= moneyToRemove)
{
money -= moneyToRemove;
return true;
}
else
{
return false;
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class SelectCharacters : MonoBehaviour
{
SelectCharacters.Data data = new SelectCharacters.Data();
private int i;
public GameObject[] AllCharacters;
public GameObject ArrowToLef;
public GameObject ArrowToRight;
public GameObject ButtonBuyCharacter;
public GameObject ButtonSelectCharacter;
public GameObject TextSelectCharacter;
private string statusCheck;
private int check;
public ShopAssist script1;
private int loadedAmount;
public Text TextPrice;
[System.Serializable]
public class Data
{
public string currentCharacter = "MainBall";
public List<string> haveCharacters = new List<string> { "MainBall" };
public int money;
}
private void Start()
{
if (PlayerPrefs.HasKey("SaveGame"))
{
data = JsonUtility.FromJson<SelectCharacters.Data>(PlayerPrefs.GetString("SaveGame"));
}
else
{
data.money = 500;
PlayerPrefs.SetString("SaveGame", JsonUtility.ToJson(data));
}
AllCharacters[i].SetActive(true);
if (data.currentCharacter == AllCharacters[i].name)
{
ButtonBuyCharacter.SetActive(false);
ButtonSelectCharacter.SetActive(false);
TextSelectCharacter.SetActive(true);
}
else if (data.currentCharacter != AllCharacters[i].name)
{
StartCoroutine(CheckHaveCharacter());
}
if (i > 0)
{
ArrowToLef.SetActive(true);
}
if (i == AllCharacters.Length)
{
ArrowToRight.SetActive(false);
}
}
private void Update()
{
data.money = ShopAssist.money;
}
public IEnumerator CheckHaveCharacter()
{
while (statusCheck != "Check")
{
if (data.haveCharacters.Count != check)
{
if (AllCharacters[i].name != data.haveCharacters[check])
{
check++;
}
else if (AllCharacters[i].name == data.haveCharacters[check])
{
TextSelectCharacter.SetActive(false);
ButtonBuyCharacter.SetActive(false);
ButtonSelectCharacter.SetActive(true);
check = 0;
statusCheck = "Check";
}
}
else if (data.haveCharacters.Count == check)
{
ButtonSelectCharacter.SetActive(false);
TextSelectCharacter.SetActive(false);
ButtonBuyCharacter.SetActive(true);
TextPrice.text = AllCharacters[i].GetComponent<Item>().priceCharacter.ToString();
check = 0;
statusCheck = "Check";
}
}
statusCheck = "";
yield return null;
}
public void SelectCharacter()
{
data = JsonUtility.FromJson<SelectCharacters.Data>(PlayerPrefs.GetString("SaveGame"));
data.currentCharacter = AllCharacters[i].name;
PlayerPrefs.SetString("SaveGame", JsonUtility.ToJson(data));
ButtonSelectCharacter.SetActive(false);
TextSelectCharacter.SetActive(true);
}
public void BuyCharacter()
{
if (data.money >= AllCharacters[i].GetComponent<Item>().priceCharacter)
{
data = JsonUtility.FromJson<SelectCharacters.Data>(PlayerPrefs.GetString("SaveGame"));
data.money = data.money - AllCharacters[i].GetComponent<Item>().priceCharacter;
data.haveCharacters.Add(AllCharacters[i].name);
PlayerPrefs.SetString("SaveGame", JsonUtility.ToJson(data));
ButtonBuyCharacter.SetActive(false);
ButtonSelectCharacter.SetActive(true);
}
}
}
Answer the question
In order to leave comments, you need to log in
How many times have you asked this question? The third? You won't learn anything if you just copy-paste code that someone else wrote for you. You found the code somewhere, copied it and try to change it without understanding how everything works.
You don't take this money from the first script, you load data from json into BuyCharacter, change it and save it.
You here and here
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question