- Home /
Text not setting to correct value
I have a couple scripts that when the purchase manager is called and you have enough money is plays a purchase sound and then it takes the money away from you, after that it calles my second script the set the coin text at the top of the gui. the purchase sound is playing fine, and the money is being reduced but only when i reload the scene. Here are my scripts
Script 1:
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;
public class PurchaseManager : MonoBehaviour {
public AudioClip purchase, cantPurchase;
private int[] ballPrices = {100,100,100,100,100,100,100,100,100,100,125,150,175,200,250,300,350,400,450,500,600,700,800,900,1000,1200,1400,1600,1800,2000,2500,3000,4000,5000,10000};
private int[] paddlePrices = {100,100,100,100,100,100,100,100,100,100,125,150,175,200,250,300,350,400,450,500,600,700,800,900,1000,1200,1400,1600,1800,2000};
private Text text;
private int arrayNom;
private string purchaseType;
private Color buttonColor;
private SetCoinText setCoinText;
private AudioSource audioSource;
private Button button;
private bool isPurchased;
private CustomiseButtons customiseButtons;
void Start () {
text = GetComponent<Text>();
setCoinText = GameObject.FindObjectOfType<SetCoinText>();
audioSource = GetComponent<AudioSource>();
button = GetComponent<Button>();
}
public void SetCost(string type, int arrayNumber, CustomiseButtons caller) {
isPurchased = false;
customiseButtons = caller;
arrayNom = arrayNumber;
purchaseType = type;
if (type == "Ball") {
if (PlayerPrefs.GetInt(arrayNumber.ToString() + type) == 0) {
text.text = "Purchase? \nCost : " + ballPrices[arrayNumber];
} else {
text.text = "Already Purchased";
isPurchased = true;
}
}
if (type == "Paddle") {
if (PlayerPrefs.GetInt(arrayNumber.ToString() + type) == 0) {
text.text = "Purchase? \nCost : " + paddlePrices[arrayNumber];
} else {
text.text = "Already Purchased";
isPurchased = true;
}
}
}
public void Purchase() {
if (purchaseType == "Ball") {
if (ballPrices[arrayNom] > PlayerPrefs.GetInt("Coins")) {
audioSource.clip = cantPurchase;
audioSource.Play();
} else {
if (PlayerPrefs.GetInt(arrayNom.ToString() + purchaseType) == 0 && !isPurchased) {
audioSource.clip = purchase;
audioSource.Play();
PlayerPrefs.SetInt("Coins", (PlayerPrefs.GetInt("Coins") - ballPrices[arrayNom]));
PlayerPrefs.SetInt(arrayNom.ToString() + purchaseType, 1);
SetCost(purchaseType, arrayNom, customiseButtons);
setCoinText.SetCoins();
}
}
}
if (purchaseType == "Paddle") {
if (paddlePrices[arrayNom] > PlayerPrefs.GetInt("Coins")) {
audioSource.clip = cantPurchase;
audioSource.Play();
} else {
if (PlayerPrefs.GetInt(arrayNom.ToString() + purchaseType) == 0 && !isPurchased) {
audioSource.clip = purchase;
audioSource.Play();
PlayerPrefs.SetInt("Coins", (PlayerPrefs.GetInt("Coins") - paddlePrices[arrayNom]));
PlayerPrefs.SetInt(arrayNom.ToString() + purchaseType, 1);
SetCost(purchaseType, arrayNom, customiseButtons);
SetCoins();
}
}
}
customiseButtons.SetColor();
}
void SetCoins() {
setCoinText.SetCoins();
}
}
And then Script 2:
using UnityEngine; using System.Collections; using UnityEngine.UI;
public class SetCoinText : MonoBehaviour {
private Text coinText;
public bool isCoins;
void Start () {
coinText = GetComponent<Text>();
SetCoins();
}
public void SetCoins() {
if (isCoins) {
print(PlayerPrefs.GetInt("Coins".ToString()));
print("?");
coinText.text = PlayerPrefs.GetInt("Coins").ToString();
} else {
coinText.text = PlayerPrefs.GetInt("Gems").ToString();
}
}
}
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
How to translate object using script? 1 Answer