Search Unity

i need help Shop game AmountMoney can't Subtraction But it increases the number

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

  1. preeyaporn110145l

    preeyaporn110145l

    Joined:
    Dec 10, 2020
    Posts:
    8
    Code (CSharp):
    1. ///////Character///   recive Value formcaracter//
    2. PlayerPrefs.SetInt("Money" +mapIndex,MoneyAmount + PlayerPrefs.GetInt("Money" +mapIndex) );
    3.  
    4. /////UI manager//////
    5. using System.Collections;
    6. using System.Collections.Generic;
    7. using UnityEngine;
    8. using UnityEngine.UI;
    9. using UnityEngine.SceneManagement;
    10. using System;
    11.  
    12. public class UIManager_2 : MonoBehaviour
    13. {
    14.     public static UIManager_2 instance;
    15.     public GameObject MapSelectionPanel;
    16.     [Header("Our STAR UI")]
    17.     public int Stars;
    18.     // public int Moneys;
    19.     public Text starText;
    20.     //public Text moneyText;
    21.     public int Moneys2;
    22.     public Text moneyText2;
    23.  
    24.  
    25.     public void Awake()
    26.     {
    27.         instance = this;
    28.         if (instance == null)
    29.         {
    30.             instance = this;
    31.         }
    32.         else
    33.         {
    34.             if (instance != this)
    35.             {
    36.                 Destroy(gameObject);
    37.             }
    38.         }
    39.         //DontDestroyOnLoad(gameObject);*/
    40.     }
    41.     public void Start()
    42.     {
    43.         //PlayerPrefs.DeleteAll();
    44.     }
    45.     public void Update()
    46.     {
    47.         UpdateStarUI();
    48.         UpdateNewMoney();
    49.     }
    50.     public void UpdateStarUI()
    51.     {
    52.         Stars = PlayerPrefs.GetInt("Star" + 1) + PlayerPrefs.GetInt("Star" + 2) + PlayerPrefs.GetInt("Star" + 3) + PlayerPrefs.GetInt("Star" + 4)
    53.             + PlayerPrefs.GetInt("Star" + 5) + PlayerPrefs.GetInt("Star" + 6) + PlayerPrefs.GetInt("Star" + 7) + PlayerPrefs.GetInt("Star" + 8); /*PlayerPrefs.GetInt("Map" + 1) + PlayerPrefs.GetInt("Map" + 2) + PlayerPrefs.GetInt("Map" + 3) + PlayerPrefs.GetInt("Map" + 4) + PlayerPrefs.GetInt("Map" + 5) + PlayerPrefs.GetInt("Map" + 6) + PlayerPrefs.GetInt("Map" + 7) + PlayerPrefs.GetInt("Map" + 8);*/
    54.         starText.text = Stars.ToString() + ("/24");
    55.     }
    56.    public void UpdateNewMoney()
    57.     {
    58.         Moneys2 = PlayerPrefs.GetInt("Money" + 1 )+ PlayerPrefs.GetInt("Money" + 2) + PlayerPrefs.GetInt("Money" + 3) + PlayerPrefs.GetInt("Money" + 4)
    59.                  + PlayerPrefs.GetInt("Money" + 5) + PlayerPrefs.GetInt("Money" + 6) + PlayerPrefs.GetInt("Money" + 7) + PlayerPrefs.GetInt("Money" + 8);
    60.         moneyText2.text = Moneys2.ToString() + (" B");
    61.     }
    62. }
    63. /////////////////////////////shop Game////
    64. using System.Collections;
    65. using System.Collections.Generic;
    66. using UnityEngine;
    67. using UnityEngine.UI;
    68. using UnityEngine.SceneManagement;
    69. using System;
    70.  
    71. public class ShopManager : MonoBehaviour
    72. {
    73.     public int Money;
    74.     public Text moneyText;
    75.     public int item1;
    76.     //public int item2;
    77.     public GameObject shop;
    78.     public int Moneys3;
    79.     // Start is called before the first frame update
    80.     void Start()
    81.     {
    82.  
    83.     }
    84.  
    85.     // Update is called once per frame
    86.     void Update()
    87.     {
    88.         UpdateMoney();
    89.        
    90.  
    91.     }
    92.     public void UpdateMoney()
    93.     {
    94.  
    95.         Money = UIManager_2.instance.Moneys2;
    96.         moneyText.text = "Money :" + Money;
    97.     }
    98.     public void BuyItem()
    99.     {
    100.         if (Money >= item1)
    101.         {
    102.             Moneys3 = Money - item1;
    103.             PlayerPrefs.SetInt("Money3", Moneys3);
    104.         }
    105.         else
    106.         {
    107.             Debug.Log("NoMoney");
    108.         }
    109.     }
    110.     public void OpenShop()
    111.     {
    112.         shop.gameObject.SetActive(true);
    113.     }
    114.     public void ExitShop()
    115.     {
    116.         shop.gameObject.SetActive(false);
    117.     }
    118. }
    119.  
     

    Attached Files:

  2. preeyaporn110145l

    preeyaporn110145l

    Joined:
    Dec 10, 2020
    Posts:
    8
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,695
    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.
     
  4. preeyaporn110145l

    preeyaporn110145l

    Joined:
    Dec 10, 2020
    Posts:
    8
    Plase Help me i try it 3days Not finished my shop
     
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,695
    I assure you the person best positioned to solve this is you, by the steps outlined above.

    If you are unwilling to do the engineering, I'm sorry, I cannot do it for you.