Coin Spending/PlayerPref Subtracting
Hello and thanks for taking a look at my issue. I currently have a self-designed Gold Collection System in which the CoinController.cs script holds the value of my coinCount integer. Then, by using CoinBehavior.cs attached to each coin, I am able to add 1 coin to my total amount with CoinController.coinCount++. That all works exactly how I want however, the problem occurs when I attempt to subtract a specific amount. For example, if my Player has 5,000 coins saved and he goes to the store to purchase a 1,000 coin item, how could I make sure that the Player has at least 1,000 coins available so that the amount can never go below 0 coins, then subtract 1,000 coins from the 5,000 coin total, and then save the new coinCount total as 4,000?
I don't need someone to write my whole code, I'm just looking for ideas, tips, and/or strategies that might help me out a little when it comes to spending the coins that have been collected by using my UI button OnClick function to call SpendCoins.cs and then "public void BuyNewCharacter". Thank you in advance for your time and the associated codes are attached below.
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using GooglePlayGames;
using UnityEngine.SocialPlatforms;
public class CoinController : MonoBehaviour
{
public static int coinCount = 0;
Text text;
void Awake()
{
text = GetComponent<Text>();
coinCount = PlayerPrefs.GetInt("Gold Count");
}
void Update()
{
text.text = "" + coinCount;
PlayerPrefs.SetInt("Gold Count", coinCount);
}
}
using UnityEngine;
using System.Collections;
public class CoinBehavior : MonoBehaviour
{
void OnTriggerEnter2D(Collider2D collider)
{
switch(collider.gameObject.name)
{
case "Player":
CoinController.coinCount++;
Destroy(this.gameObject);
break;
}
}
}
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using GooglePlayGames;
using UnityEngine.SocialPlatforms;
public class SpendCoins : MonoBehaviour
{
public static int coinCount;
public static int NewCharacterCost = 1000;
public static int GoldLeft;
public static int coinsLeft;
void Awake()
{
coinsLeft = PlayerPrefs.GetInt("Gold Count");
}
public void BuyNewCharacter()
{
if(coinsLeft > 1000)
{
GoldLeft = coinsLeft - NewCharacterCost;
CoinController.coinCount = GoldLeft;
PlayerPrefs.SetInt("Gold Count", coinCount);
}
}
}
Well, I can give you some tips, but i have read your code, and don't see a problem! $$anonymous$$ainly, what your doing is sound, so what exactly is the problem, or what is not working right?
I deleted my last reply because I just figured out how to let you know about the problem I am running into. When that button is pressed, its supposed to reassign the value for how many coins are left after the 1,000 has been subtracted. But, if I use the same lines of code from the "public void BuyNewCharacter" button on another button, lets say if I had a "BuyGirlCharacter" button for example, and I change it to
public static int GirlCharacterCost = 2000;
public void BuyGirlCharacter()
{
if(coinsLeft > 2000)
{
GoldLeft = coinsLeft - GirlCharacterCost;
CoinController.coinCount = GoldLeft;
PlayerPrefs.SetInt("Gold Count", coinCount);
}
then it subtracts from the original amount and doesn't take into consideration the fact that I had already subtracted 1,000. Something isn't right and I know I'm just overlooking some tiny detail.
Each UI button in theory should:
Check if there is enough gold in "Gold Count" for the purchase.
Subtract the character cost from the amount in "Gold Count".
Re-save the new amount left over in "Gold Count".
Update the CoinController so that coin text is displayed correctly.
Disable the button to prevent multiple purchase.
Hopefully that makes sense. I think that using the same public static integer in multiple places might be causing it but I just can't wrap my head around the logic for this. I've overcome some ridiculous obstacles, but this one is just irritating. Thank again.
@Sp33dy_Sn1pa Hey, I've thought about what you said, and I have a solution. If you want a button to remove cash, you should make sure that there is only one script and one place that removes it.
example: int currentCash = PlayerPrefs.GetInt ( current cash); int cost = 1,000;
if(currentCash >= 1000) { currentCash = currentCash - cost;
PlayerPrefs.SetInt (currentCash);
}
And Here's How I Think You Should Disable The Button: (This is)
Button button; Text buttonText;
void Start () { button = GameObject.Find("THE NA$$anonymous$$E OF YOUR BUTTON").GetComponent(); buttonText = GameObject.Find("NA$$anonymous$$E OF BUTTON/NA$$anonymous$$E OF TEXT").GetComponent(); }
public void DisableButton() { button.interactable = false; buttonText.text = "Already Bought";
}
Thank you so much for your assistance! I thought about what you said in regard to having it on the same script and so what I was able to come up with was
public static int coinCount = 0;
public static int character1Cost = 1000;
public static int character2Cost = 3000;
public static int character3Cost = 5000;
public static int goldRemaining;
Text text;
void Awake()
{
text = GetComponent<Text>();
coinCount = PlayerPrefs.GetInt("Gold Count");
}
public void BuyCharacterOne()
{
if(coinCount >= 1000)
{
goldRemaining = coinCount - character1Cost;
coinCount = goldRemaining;
}
}
public void BuyCharacterTwo()
{
if(coinCount >= 3000)
{
goldRemaining = coinCount - character2Cost;
coinCount = goldRemaining;
}
}
public void BuyCharacterThree()
{
if(coinCount >= 5000)
{
goldRemaining = coinCount - character3Cost;
coinCount = goldRemaining;
}
}
void Update()
{
text.text = "" + coinCount;
PlayerPrefs.SetInt("Gold Count", coinCount);
}
}