Search Unity

I am having a problem with my money updates when substraction them.

Discussion in 'Scripting' started by preeyaporn110145l, Dec 13, 2020.

  1. preeyaporn110145l

    preeyaporn110145l

    Joined:
    Dec 10, 2020
    Posts:
    8
    Code (CSharp):
    1. public class UIManager_2 : MonoBehaviour
    2. {
    3.     public static UIManager_2 instance;
    4.     public GameObject MapSelectionPanel;
    5.     [Header("Our STAR UI")]
    6.     public int Stars;
    7.  
    8.     public Text starText;
    9.  
    10.     public int Moneys2;
    11.     public int Moneys3;
    12.     public Text moneyText2;
    13.  
    14.     public int Item1;
    15.     public GameObject shopPanel;
    16.     public Text Priceitem;
    17.     public Text myMoney;
    18.  
    19.  
    20.  
    21.     public void Awake()
    22.     {
    23.         instance = this;
    24.  
    25.     }
    26.  
    27.     public void Update()
    28.     {
    29.         UpdateStarUI();
    30.         UpdateNewMoney();
    31.         moneyText2.text = Moneys2.ToString() + (" B");
    32.         myMoney.text = Moneys2.ToString() + (" B");
    33.         Priceitem.text = "Price :"+Item1.ToString() ;
    34.         Updatemoney3();
    35.     }
    36.     public void UpdateStarUI()
    37.     {
    38.         Stars = PlayerPrefs.GetInt("Star" + 1) + PlayerPrefs.GetInt("Star" + 2) + PlayerPrefs.GetInt("Star" + 3) + PlayerPrefs.GetInt("Star" + 4)
    39.             + PlayerPrefs.GetInt("Star" + 5) + PlayerPrefs.GetInt("Star" + 6) + PlayerPrefs.GetInt("Star" + 7) + PlayerPrefs.GetInt("Star" + 8);
    40.         starText.text = Stars.ToString() + ("/24");
    41.     }
    42.     public void UpdateNewMoney()
    43.     {    
    44.         Moneys2 = PlayerPrefs.GetInt("Money" + 1) + PlayerPrefs.GetInt("Money" + 2) + PlayerPrefs.GetInt("Money" + 3) + PlayerPrefs.GetInt("Money" + 4)
    45.           + PlayerPrefs.GetInt("Money" + 5) + PlayerPrefs.GetInt("Money" + 6) + PlayerPrefs.GetInt("Money" + 7) + PlayerPrefs.GetInt("Money" + 8);
    46.         Debug.Log("Money" + Moneys2);
    47.     }
    48.  
    49.  
    50.     public void BuyItem()
    51.     {
    52.  
    53.         if (Moneys2 >= Item1)
    54.         {
    55.             Moneys3 = Moneys2 - Item1;
    56.             //PlayerPrefs.SetInt("Money",Moneys2);
    57.             Debug.Log("MoneySubStrac :" + Moneys3);
    58.         }
    59.         else
    60.         {
    61.             Debug.Log("NoMoney");
    62.         }
    63.      
    64.     }
    65.     public void Updatemoney3()
    66.     {
    67.         Moneys3 += Moneys2;
    68.         Debug.Log("newMoney :" + Moneys3);
    69.     }

    I want to earn money from external classes using PlayerPrefs and subtract the value of the item. I have been chasing for several days and don't understand.
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,742
    To help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

    Doing this should help you answer these types of questions:

    - is this code even running? which parts are running? how often does it run?
    - what are the values of the variables involved? Are they initialized?

    Knowing this information will help you reason about the behavior you are seeing.
     
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,742
    Also, what are you doing in line 44? Do you really want lots of PlayerPrefs integers called "Money1" , "Money2" etc.??

    You might want to use an array and get it all working WITHOUT player prefs, then when it works put the array into another class, serialize it to a string with JSON, and write that to a single PlayerPrefs string.
     
  4. preeyaporn110145l

    preeyaporn110145l

    Joined:
    Dec 10, 2020
    Posts:
    8

    Code (CSharp):
    1. /// main class ///////
    2. private void OnTriggerEnter(Collider hit)
    3.     {
    4.         if (hit.CompareTag ("Coin"))
    5.         {
    6.             collidedCoinValue = hit.gameObject.GetComponent<CoinValue>().coinValue1;
    7.             MoneyAmount += collidedCoinValue;
    8.             collidedCoinValue2 = hit.gameObject.GetComponent<CoinValue>().coinValue2;
    9.             CoinAmount += collidedCoinValue2;
    10.             Destroy(hit.gameObject);
    11.         }
    12.     }
    13. PlayerPrefs.SetInt("Money" + mapIndex, MoneyAmount + PlayerPrefs.GetInt("Money" + mapIndex));
    14.  
    15. /////recive class///
    16. public class UIManager_2 : MonoBehaviour
    17. {
    18.     public static UIManager_2 instance;
    19.     public GameObject MapSelectionPanel;
    20.     [Header("Our STAR UI")]
    21.     public int Stars;
    22.  
    23.     public Text starText;
    24.  
    25.     public int Moneys2;
    26.     public Text moneyText2;
    27.     public GameObject shopPanel;
    28.     public Text Priceitem;
    29.     public int Item1;
    30.     public Text myMoney;
    31.  
    32.  
    33.  
    34.     public void Awake()
    35.     {
    36.         instance = this;
    37.     }
    38.     private void Start()
    39.     {
    40.         Moneys2 = PlayerPrefs.GetInt("Money" + 1) + PlayerPrefs.GetInt("Money" + 2) + PlayerPrefs.GetInt("Money" + 3) + PlayerPrefs.GetInt("Money" + 4)
    41.     + PlayerPrefs.GetInt("Money" + 5) + PlayerPrefs.GetInt("Money" + 6) + PlayerPrefs.GetInt("Money" + 7) + PlayerPrefs.GetInt("Money" + 8);
    42.         PlayerPrefs.SetInt("Money", Moneys2);
    43.         Debug.Log("Money :" + PlayerPrefs.GetInt("Money"));
    44.     }
    45.     public void Update()
    46.     {
    47.         UpdateStarUI();
    48.          moneyText2.text = PlayerPrefs.GetInt("Money").ToString() + (" B");
    49.         myMoney.text = PlayerPrefs.GetInt("Money").ToString() + (" B");
    50.         Priceitem.text = "Price :"+Item1.ToString() ;
    51.     }
    52.     public void UpdateStarUI()
    53.     {
    54.         Stars = PlayerPrefs.GetInt("Star" + 1) + PlayerPrefs.GetInt("Star" + 2) + PlayerPrefs.GetInt("Star" + 3) + PlayerPrefs.GetInt("Star" + 4)
    55.             + PlayerPrefs.GetInt("Star" + 5) + PlayerPrefs.GetInt("Star" + 6) + PlayerPrefs.GetInt("Star" + 7) + PlayerPrefs.GetInt("Star" + 8);
    56.         starText.text = Stars.ToString() + ("/24");
    57.     }
    58.  
    59.     public void BuyItem()
    60.     {
    61.      
    62.         if (PlayerPrefs.GetInt("Money") >= Item1)
    63.         {
    64.             PlayerPrefs.SetInt("Money", PlayerPrefs.GetInt("Money")-Item1);
    65.  
    66.               Debug.Log("MoneySub :" + PlayerPrefs.GetInt("Money"));
    67.          
    68.         }
    69.         else
    70.         {
    71.             Debug.Log("NoMoney");
    72.         }
    73.      
    74.     }


    Should I use PlayerPrefs? Or use other than this What should I use to collect my money?
     
    Last edited: Dec 14, 2020