Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Feedback Is my simple shop code is good enough?

Discussion in 'Scripting' started by JustAsking06, Feb 23, 2023.

  1. JustAsking06

    JustAsking06

    Joined:
    Aug 11, 2021
    Posts:
    38
    I changed my code based on suggestions from the previous thread and it became much more organized. Do you think it's good now or does it still need editing? (I'd appreciate it if you just say it in terms of performance.)

    I have done enough research on the save system and even opened a topic in the forum and I decided to continue with playerprefs since I will not store much data in this project. So you don't have to say it, thank you


    Shop Manager Script:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.EventSystems;
    5. using UnityEngine.UI;
    6.  
    7. public class ShopManager : MonoBehaviour
    8. {
    9.     private GameManager gameManager;
    10.     [SerializeField] private Button[] gunPanels;
    11.  
    12.     private void Start()
    13.     {
    14.         gameManager = GetComponent<GameManager>();
    15.     }
    16.     public void Buy()
    17.     {
    18.         GameObject buttonRef = GameObject.FindGameObjectWithTag("Event").GetComponent<EventSystem>().currentSelectedGameObject;
    19.         ButtonInfo buttonInfo = buttonRef.GetComponent<ButtonInfo>();
    20.  
    21.         if (PlayerPrefs.GetFloat("PlayerMoney", 0) >= buttonInfo.itemPrice)
    22.         {
    23.             gameManager.AddMoney(-buttonInfo.itemPrice);
    24.             buttonInfo.BuyGun();
    25.         }
    26.     }
    27.     public void Use()
    28.     {
    29.         for (int i = 0; i < gunPanels.Length; i++)
    30.         {
    31.             if ((PlayerPrefs.GetInt("UseButton" + i, 0) == 1))
    32.             {
    33.                 gunPanels[i].GetComponent<GunUseButton>().StopUse();
    34.             }
    35.         }
    36.         GameObject buttonRef = GameObject.FindGameObjectWithTag("Event").GetComponent<EventSystem>().currentSelectedGameObject;
    37.         GunUseButton useButtonRef = buttonRef.GetComponent<GunUseButton>();
    38.         useButtonRef.Use();
    39.     }
    40.     public void CloseTab()
    41.     {
    42.         GameObject.FindGameObjectWithTag("Event").GetComponent<EventSystem>().currentSelectedGameObject
    43.             .transform.parent.gameObject.SetActive(false);
    44.     }
    45. }
    46.  
    ButtonInfo Script Attached to buy buttons

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5. using TMPro;
    6.  
    7. public class ButtonInfo : MonoBehaviour
    8. {
    9.     public int itemID;
    10.     public float itemPrice;
    11.     public bool usingItem;
    12.     private TextMeshProUGUI priceText;
    13.  
    14.     private void Start()
    15.     {
    16.         if (PlayerPrefs.GetInt("BoughtGun" + itemID, 0) == 1) BuyGun();
    17.         else
    18.         {
    19.             priceText = GetComponentInChildren<TextMeshProUGUI>();
    20.             transform.parent.GetComponent<Button>().enabled = false;   //Set panel objects button false to not be able to use it
    21.             UpdateText(itemPrice, priceText);
    22.         }
    23.     }
    24.     public void BuyGun()
    25.     {
    26.         transform.parent.GetComponent<Button>().enabled = true;
    27.         PlayerPrefs.SetInt("BoughtGun" + itemID, 1);
    28.         Destroy(gameObject);
    29.     }
    30.     public void UpdateText(float amount, TextMeshProUGUI text)
    31.     {
    32.         string[] ScoreNames = new string[] { "", "k", "M", "B", "T", "KK", "MM", "TT", "ad", "ae", "af", "ag", "ah", "ai", "aj", "ak", "al", "am", "an", "ao", "ap", "aq", "ar", "as", "at", "au", "av", "aw", "ax", "ay", "az", "ba", "bb", "bc", "bd", "be", "bf", "bg", "bh", "bi", "bj", "bk", "bl", "bm", "bn", "bo", "bp", "bq", "br", "bs", "bt", "bu", "bv", "bw", "bx", "by", "bz", };
    33.         int i;
    34.  
    35.         for (i = 0; i < ScoreNames.Length; i++)
    36.             if (amount < 999)
    37.                 break;
    38.             else amount = Mathf.Floor(amount / 100f) / 10f;
    39.  
    40.         if (amount == Mathf.Floor(amount))
    41.             text.text = amount.ToString() + ScoreNames[i];
    42.         else text.text = amount.ToString("F1") + ScoreNames[i];
    43.  
    44.     }
    45. }
    46.  
    Use Button Script Which is attached to panel button (i made this seperate from buttoninfo script because when i destroy button script is getting destroyed to)


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class GunUseButton : MonoBehaviour
    7. {
    8.     public int buttonID;
    9.     private Button currentButton;
    10.     [SerializeField] private WeaponManager weaponManager;
    11.  
    12.     private void Start()
    13.     {
    14.         currentButton = GetComponent<Button>();
    15.         if (PlayerPrefs.GetInt("UseButton" + buttonID, 0) == 1) Use();
    16.     }
    17.     private void Update()
    18.     {
    19.  
    20.     }
    21.     public void Use()
    22.     {
    23.         currentButton.interactable = false;
    24.         PlayerPrefs.SetInt("UseButton" + buttonID, 1);
    25.         weaponManager.ChangeWeapon(buttonID);
    26.     }
    27.     public void StopUse()
    28.     {
    29.         currentButton.interactable = true;
    30.         PlayerPrefs.SetInt("UseButton" + buttonID, 0);
    31.     }
    32. }
    33.  
     
  2. All_American

    All_American

    Joined:
    Oct 14, 2011
    Posts:
    1,528
    Does it do what you want it to do? if yes, then, yes it is.
     
  3. JustAsking06

    JustAsking06

    Joined:
    Aug 11, 2021
    Posts:
    38
    Well actually my old script also was doing everything i want but it was very bad as you see. I try to write best code i can but i dont know much about it thats why i cant see the missing parts.

    Old Code was this:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5. using TMPro;
    6.  
    7. public class ShopManager : MonoBehaviour
    8. {
    9.     //Referances
    10.     [SerializeField] private MoneyManager moneyManager;
    11.     [SerializeField] private WeaponManager weaponManager;
    12.     [SerializeField] private Player playerScript;
    13.     [SerializeField] private PlayerMovement playerMovement;
    14.  
    15.     //GUN SHOP
    16.     [SerializeField] private Button[] gunUseButtons;
    17.     [SerializeField] private GameObject[] gunBuyButtons;
    18.     [SerializeField] private Gun[] guns;
    19.     [SerializeField] private TextMeshProUGUI[] gunPriceTexts;
    20.  
    21.  
    22.     [SerializeField] private TextMeshProUGUI playerBitcoinText;
    23.     //UPGRADE SHOP
    24.     [SerializeField] private int maxPlayerCapacity, maxPlayerSpeed, maxPlayerHealth;
    25.     [SerializeField] private TextMeshProUGUI playerCapacityPriceText, playerSpeedPriceText, playerHealthPriceText;
    26.     [SerializeField] private TextMeshProUGUI currentPlayerCapacityText, currentPlayerSpeedText, currentPlayerHealthText;
    27.     private int playerCapacity;
    28.     private float playerHealth;
    29.     private float playerSpeed;
    30.     [SerializeField] private float playerAddCapacityPrice, playerAddSpeedPrice, playerAddHealthPrice;
    31.  
    32.     //BOT SHOP
    33.     //GUARD ADD
    34.     [SerializeField] private GameObject guardPrefab;
    35.     [SerializeField] private Transform[] guardSpawnPositions;
    36.     private int currentGuardCount = 0;
    37.     [SerializeField] private float addGuardPrice;
    38.     [SerializeField] private TextMeshProUGUI guardCurrentText, guardPriceText;
    39.     //MONEY Bot ADD
    40.     [SerializeField] private Transform moneyDropPlace; //To Add Referance to Bot Script
    41.     [SerializeField] private GameObject moneyBotPrefab;
    42.     [SerializeField] private int maxMoneyBot;
    43.     private int currentMoneyBotCount = 0;
    44.     [SerializeField] private float addMoneyBotPrice;
    45.     [SerializeField] private TextMeshProUGUI currentMoneyBotText, moneyBotPriceText;
    46.     //Money Bot Capacity Add
    47.     [SerializeField] private TextMeshProUGUI moneyBotCapacityPriceText, currentMoneyBotCapacityText;
    48.     [SerializeField] private float moneyBotAddCapacityPrice;
    49.     [SerializeField] private int startMoneyBotCapacity, moneyBotMaxCapacity;
    50.  
    51.  
    52.  
    53.  
    54.     private void Start()
    55.     {
    56.         CheckUpgradeShopStats();
    57.         CheckGunShopStats();
    58.     }
    59.     private void CheckUpgradeShopStats()
    60.     {
    61.         //Capacity
    62.         playerCapacity = PlayerPrefs.GetInt("PlayerCapacity", 15);
    63.         playerAddCapacityPrice = PlayerPrefs.GetFloat("playerCapacityStartPrice", playerAddCapacityPrice);
    64.         UpdatePriceText(playerAddCapacityPrice, playerCapacityPriceText);
    65.         currentPlayerCapacityText.text = "CURRENT = " + playerCapacity.ToString();
    66.         if (playerCapacity >= maxPlayerCapacity) playerCapacityPriceText.text = "MAX";
    67.  
    68.         //Speed
    69.         playerSpeed = PlayerPrefs.GetFloat("PlayerSpeed", 4);
    70.         playerAddSpeedPrice = PlayerPrefs.GetFloat("playerSpeedStartPrice", playerAddSpeedPrice);
    71.         UpdatePriceText(playerAddSpeedPrice, playerSpeedPriceText);
    72.         currentPlayerSpeedText.text = "CURRENT = " + playerSpeed.ToString();
    73.         if (playerSpeed >= maxPlayerSpeed) currentPlayerSpeedText.text = "MAX";
    74.  
    75.         //Health
    76.         playerHealth = PlayerPrefs.GetFloat("PlayerHealth", 1000);
    77.         playerAddHealthPrice = PlayerPrefs.GetFloat("playerHealthStartPrice", playerAddHealthPrice);
    78.         UpdatePriceText(playerAddHealthPrice, playerHealthPriceText);
    79.         currentPlayerHealthText.text = "CURRENT = " + playerHealth.ToString();
    80.         if (playerHealth >= maxPlayerHealth) currentPlayerHealthText.text = "MAX";
    81.  
    82.         //BOT SHOP
    83.         //ADD Guard
    84.         currentGuardCount = PlayerPrefs.GetInt("GuardCount", 0);
    85.         addGuardPrice = PlayerPrefs.GetFloat("GuardPrice", addGuardPrice);
    86.         guardCurrentText.text = "CURRENT = " + currentGuardCount.ToString();
    87.         UpdatePriceText(addGuardPrice, guardPriceText);
    88.         if (currentGuardCount >= guardSpawnPositions.Length) guardCurrentText.text = "MAX";
    89.  
    90.         for (int i = 0; i < currentGuardCount; i++)
    91.         {
    92.             Instantiate(guardPrefab, guardSpawnPositions[i].transform.position, Quaternion.identity);
    93.         }
    94.  
    95.         //ADD MONEY BOT
    96.         currentMoneyBotCount = PlayerPrefs.GetInt("MoneyBotCount", 0);
    97.         addMoneyBotPrice = PlayerPrefs.GetFloat("MoneyBotPrice", addMoneyBotPrice);
    98.         currentMoneyBotText.text = "CURRENT = " + currentMoneyBotCount.ToString();
    99.         UpdatePriceText(addMoneyBotPrice, moneyBotPriceText);
    100.         if (currentMoneyBotCount >= maxMoneyBot) currentMoneyBotText.text = "MAX";
    101.  
    102.         for (int i = 0; i < currentMoneyBotCount; i++)
    103.         {
    104.             GameObject newBot = Instantiate(moneyBotPrefab, Vector3.zero+ new Vector3(i,0,-6), Quaternion.identity);
    105.             newBot.GetComponent<MoneyBotScript>().moneyDropPlace = moneyDropPlace;
    106.         }
    107.  
    108.         //Capacity
    109.         startMoneyBotCapacity = PlayerPrefs.GetInt("MoneyBotCapacity", startMoneyBotCapacity);
    110.         moneyBotAddCapacityPrice = PlayerPrefs.GetFloat("MoneyBotCapacityStartPrice", moneyBotAddCapacityPrice);
    111.         MoneyBotScript.maxMoneyCarry = startMoneyBotCapacity;
    112.         UpdatePriceText(moneyBotAddCapacityPrice, moneyBotCapacityPriceText);
    113.         currentMoneyBotCapacityText.text = "CURRENT = " + startMoneyBotCapacity.ToString();
    114.         if (startMoneyBotCapacity >= maxMoneyBot) moneyBotCapacityPriceText.text = "MAX";
    115.  
    116.     }
    117.     private void CheckGunShopStats()
    118.     {
    119.         //Set Gun Prices
    120.         for (int i = 0; i < guns.Length; i++)
    121.         {
    122.             UpdatePriceText(guns[i].price, gunPriceTexts[i]);
    123.         }
    124.         //USE SAVED WEAPON // IF NOT ACTIVE FIRST WEAPON
    125.         PlayerPrefs.SetInt("BoughtWeapon" + 0, 1);
    126.         UseWeapon(PlayerPrefs.GetInt("UsingWeapon", 0));
    127.  
    128.         //Deactive use buttons if we didnt bought weapon
    129.         for (int i = 0; i < gunUseButtons.Length; i++)
    130.         {
    131.             if (PlayerPrefs.GetInt("BoughtWeapon" + i, 0) == 1)
    132.             {
    133.                 gunUseButtons[i].enabled = true;
    134.                 Destroy(gunBuyButtons[i]);
    135.             }
    136.             else
    137.             {
    138.                 gunUseButtons[i].enabled = false;
    139.             }
    140.         }
    141.     }
    142.     private void UpdatePriceText(float amount, TextMeshProUGUI text)
    143.     {
    144.         //Update Price Text
    145.         string[] ScoreNames = new string[] { "", "k", "M", "B", "T", "KK", "MM", "TT", "ad", "ae", "af", "ag", "ah", "ai", "aj", "ak", "al", "am", "an", "ao", "ap", "aq", "ar", "as", "at", "au", "av", "aw", "ax", "ay", "az", "ba", "bb", "bc", "bd", "be", "bf", "bg", "bh", "bi", "bj", "bk", "bl", "bm", "bn", "bo", "bp", "bq", "br", "bs", "bt", "bu", "bv", "bw", "bx", "by", "bz", };
    146.         int i;
    147.  
    148.         for (i = 0; i < ScoreNames.Length; i++)
    149.             if (amount < 999)
    150.                 break;
    151.             else amount = Mathf.Floor(amount / 100f) / 10f;
    152.  
    153.         if (amount == Mathf.Floor(amount))
    154.             text.text = amount.ToString() + ScoreNames[i];
    155.         else text.text = amount.ToString("F1") + ScoreNames[i];
    156.     }
    157.     public void BuyWeapon(int ButtonID)
    158.     {
    159.         //WE SET GUN PRICES MANUALY FROM SCRIPTABLE OBJECTS AND BUTTON TEXTS
    160.         float currentMoney = PlayerPrefs.GetFloat("playerMoney", 0);
    161.         if (guns[ButtonID].price <= currentMoney )
    162.         {
    163.             currentMoney -= guns[ButtonID].price;
    164.             PlayerPrefs.SetFloat("playerMoney", currentMoney);
    165.             moneyManager.UpdateMoneyText();
    166.             BuyWeaponAction(ButtonID);
    167.         }
    168.     }
    169.     private void BuyWeaponAction(int ButtonID)
    170.     {
    171.         PlayerPrefs.SetInt("BoughtWeapon" + ButtonID, 1);
    172.         gunUseButtons[ButtonID].enabled = true;
    173.         Destroy(gunBuyButtons[ButtonID]);
    174.         UseWeapon(ButtonID);
    175.  
    176.     }
    177.     public void UseWeapon(int ButtonID)
    178.     {
    179.         //Unequip ALL WEAPONS
    180.         for (int i = 0; i < gunUseButtons.Length; i++)
    181.         {
    182.             gunUseButtons[i].interactable = true;
    183.         }
    184.         //Then Equip Current Weapon
    185.         gunUseButtons[ButtonID].interactable = false;
    186.         PlayerPrefs.SetInt("UsingWeapon", ButtonID);
    187.         weaponManager.ChangeWeapon(ButtonID);
    188.     }
    189.     public void AddCapacity()
    190.     {
    191.         float currentBitcoin = PlayerPrefs.GetFloat("playerBitcoin", 0);
    192.         if (playerCapacity < maxPlayerCapacity && currentBitcoin >= playerAddCapacityPrice)
    193.         {
    194.             //ADD Capacity
    195.             playerCapacity++;
    196.             PlayerPrefs.SetInt("PlayerCapacity", playerCapacity);
    197.             playerScript.UpdatePlayerCapacity();
    198.             currentPlayerCapacityText.text = "CURRENT = " + playerCapacity.ToString();
    199.             //Change Price
    200.             currentBitcoin -= playerAddCapacityPrice;
    201.             PlayerPrefs.SetFloat("playerBitcoin", currentBitcoin);
    202.             UpdatePriceText(currentBitcoin, playerBitcoinText);
    203.             //Make Button Price X2
    204.             playerAddCapacityPrice = playerAddCapacityPrice * 2;
    205.             UpdatePriceText(playerAddCapacityPrice, playerCapacityPriceText);
    206.             PlayerPrefs.SetFloat("playerCapacityStartPrice", playerAddCapacityPrice);
    207.  
    208.             if (playerCapacity >= maxPlayerCapacity) playerCapacityPriceText.text = "MAX";
    209.         }
    210.     }
    211.     public void AddSpeed()
    212.     {
    213.         float currentBitcoin = PlayerPrefs.GetFloat("playerBitcoin", 0);
    214.         if (playerSpeed < maxPlayerSpeed && currentBitcoin >= playerAddSpeedPrice)
    215.         {
    216.             //ADD Speed
    217.             playerSpeed += 0.5f;
    218.             PlayerPrefs.SetFloat("PlayerSpeed", playerSpeed);
    219.             playerMovement.UpdatePlayeSpeed();
    220.             currentPlayerSpeedText.text = "CURRENT = " + playerSpeed.ToString();
    221.             //Change Price
    222.             currentBitcoin -= playerAddSpeedPrice;
    223.             PlayerPrefs.SetFloat("playerBitcoin", currentBitcoin);
    224.             UpdatePriceText(currentBitcoin, playerBitcoinText);
    225.             //Make Button Price X2
    226.             playerAddSpeedPrice = playerAddSpeedPrice * 2;
    227.             UpdatePriceText(playerAddSpeedPrice, playerSpeedPriceText);
    228.             PlayerPrefs.SetFloat("playerSpeedStartPrice", playerAddSpeedPrice);
    229.  
    230.             if (playerSpeed >= maxPlayerSpeed) playerSpeedPriceText.text = "MAX";
    231.         }
    232.     }
    233.     public void AddHealth()
    234.     {
    235.         float currentBitcoin = PlayerPrefs.GetFloat("playerBitcoin", 0);
    236.         if (playerHealth < maxPlayerHealth && currentBitcoin >= playerAddHealthPrice)
    237.         {
    238.             //ADD Health
    239.             playerHealth+= 100;
    240.             PlayerPrefs.SetFloat("PlayerHealth", playerHealth);
    241.             playerScript.UpdatePlayerHealth();
    242.             currentPlayerHealthText.text = "CURRENT = " + playerHealth.ToString();
    243.             //Change Price
    244.             currentBitcoin -= playerAddHealthPrice;
    245.             PlayerPrefs.SetFloat("playerBitcoin", currentBitcoin);
    246.             UpdatePriceText(currentBitcoin, playerBitcoinText);
    247.             //Make Button Price X2
    248.             playerAddHealthPrice = playerAddHealthPrice * 2;
    249.             UpdatePriceText(playerAddHealthPrice, playerHealthPriceText);
    250.             PlayerPrefs.SetFloat("playerHealthStartPrice", playerAddHealthPrice);
    251.  
    252.             if (playerHealth >= maxPlayerHealth) playerHealthPriceText.text = "MAX";
    253.         }
    254.     }
    255.     public void AddGuard()
    256.     {
    257.         float currentBitcoin = PlayerPrefs.GetFloat("playerBitcoin", 0);
    258.  
    259.         if (currentGuardCount < guardSpawnPositions.Length && currentBitcoin >= addGuardPrice)
    260.         {
    261.             //ADD Guard
    262.             PlayerPrefs.SetInt("GuardCount", currentGuardCount);
    263.             Instantiate(guardPrefab, guardSpawnPositions[currentGuardCount].transform.position, Quaternion.identity);
    264.             currentGuardCount++;
    265.             //Change Price
    266.             currentBitcoin -= addGuardPrice;
    267.             PlayerPrefs.SetFloat("playerBitcoin", currentBitcoin);
    268.             UpdatePriceText(currentBitcoin, playerBitcoinText);
    269.             addGuardPrice = addGuardPrice * 2;
    270.             PlayerPrefs.SetFloat("GuardPrice", addGuardPrice);
    271.             UpdatePriceText(addGuardPrice, guardPriceText);
    272.             if (currentGuardCount >= guardSpawnPositions.Length) guardPriceText.text = "MAX";
    273.             //Change Count Text
    274.             guardCurrentText.text = "CURRENT = " + currentGuardCount.ToString();
    275.         }
    276.  
    277.     }
    278.     public void AddMoneyBot()
    279.     {
    280.         float currentBitcoin = PlayerPrefs.GetFloat("playerBitcoin", 0);
    281.  
    282.         if (currentMoneyBotCount < maxMoneyBot && currentBitcoin >= addMoneyBotPrice)
    283.         {
    284.             //ADD MoneyBot
    285.             PlayerPrefs.SetInt("MoneyBotCount", currentMoneyBotCount);
    286.             GameObject newBot = Instantiate(moneyBotPrefab, Vector3.zero, Quaternion.identity);
    287.             newBot.GetComponent<MoneyBotScript>().moneyDropPlace = moneyDropPlace;
    288.             currentMoneyBotCount++;
    289.             //Change Price
    290.             currentBitcoin -= addMoneyBotPrice;
    291.             PlayerPrefs.SetFloat("playerBitcoin", currentBitcoin);
    292.             UpdatePriceText(currentBitcoin, playerBitcoinText);
    293.             addMoneyBotPrice = addMoneyBotPrice * 2;
    294.             PlayerPrefs.SetFloat("MoneyBotPrice", addMoneyBotPrice);
    295.             UpdatePriceText(addMoneyBotPrice, moneyBotPriceText);
    296.             if (currentMoneyBotCount >= maxMoneyBot) moneyBotPriceText.text = "MAX";
    297.             //Change Count Text
    298.             currentMoneyBotText.text = "CURRENT = " + currentMoneyBotCount.ToString();
    299.         }
    300.  
    301.     }
    302.     public void AddBotCapacity()
    303.     {
    304.         float currentBitcoin = PlayerPrefs.GetFloat("playerBitcoin", 0);
    305.         if (startMoneyBotCapacity < moneyBotMaxCapacity && currentBitcoin >= playerAddCapacityPrice)
    306.         {
    307.             //ADD Capacity
    308.             startMoneyBotCapacity++;
    309.             PlayerPrefs.SetInt("MoneyBotCapacity", startMoneyBotCapacity);
    310.             MoneyBotScript.maxMoneyCarry = startMoneyBotCapacity;
    311.             currentMoneyBotCapacityText.text = "CURRENT = " + startMoneyBotCapacity.ToString();
    312.             //Change Price
    313.             currentBitcoin -= moneyBotAddCapacityPrice;
    314.             PlayerPrefs.SetFloat("playerBitcoin", currentBitcoin);
    315.             UpdatePriceText(currentBitcoin, playerBitcoinText);
    316.             //Make Button Price X2
    317.             moneyBotAddCapacityPrice = moneyBotAddCapacityPrice * 2;
    318.             UpdatePriceText(moneyBotAddCapacityPrice, moneyBotCapacityPriceText);
    319.             PlayerPrefs.SetFloat("MoneyBotCapacityStartPrice", moneyBotAddCapacityPrice);
    320.  
    321.             if (startMoneyBotCapacity >= moneyBotMaxCapacity) moneyBotCapacityPriceText.text = "MAX";
    322.         }
    323.     }
    324.  
    325. }
    326.  
     
  4. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    7,606
    Lines like this:
    Code (CSharp):
    1. GameObject buttonRef = GameObject.FindGameObjectWithTag("Event").GetComponent<EventSystem>().currentSelectedGameObject;
    And this:
    Code (CSharp):
    1. GameObject.FindGameObjectWithTag("Event").GetComponent<EventSystem>().currentSelectedGameObject.transform.parent.gameObject.SetActive(false);
    Are unnecessary. The event system has a singleton instance you can readily access. There's no need to repeatedly find it. I also wonder what's the point of these segments in general. What's calling these? Why does it need to find the currently selected button to gleam info of it?
     
  5. JustAsking06

    JustAsking06

    Joined:
    Aug 11, 2021
    Posts:
    38
    LOL thank you a lot. I didnt know that i dont need to find it, now its a lot better thanks to you. I copied that part from one tutorial video so i thought there must be a point that he is finding with tag.

    Code (CSharp):
    1.     [SerializeField] private EventSystem eventSystem;
    2.     public void Buy()
    3.     {
    4.         ButtonInfo buttonInfo = eventSystem.currentSelectedGameObject.GetComponent<ButtonInfo>();
    5.  
    6.         if (PlayerPrefs.GetFloat("PlayerMoney", 0) >= buttonInfo.itemPrice)
    7.         {
    8.             gameManager.AddMoney(-buttonInfo.itemPrice);
    9.             buttonInfo.BuyGun();
    10.         }
    11.     }
    12.     public void Use()
    13.     {
    14.         for (int i = 0; i < gunPanels.Length; i++)
    15.         {
    16.             if ((PlayerPrefs.GetInt("UseButton" + i, 0) == 1))
    17.             {
    18.                 gunPanels[i].GetComponent<GunUseButton>().StopUse();
    19.             }
    20.         }
    21.         eventSystem.currentSelectedGameObject.GetComponent<GunUseButton>().Use();
    22.     }
    There is scripts attached to butons like ButtonInfo script and i check price and some other variables from it. Thats why i check current selected button so i can read its scripts values.
     
  6. JustAsking06

    JustAsking06

    Joined:
    Aug 11, 2021
    Posts:
    38
    I want to use only one item at the time thats why i made a looping in Use Void that unuse all buttons, do you think its okey to loop like that?
     
  7. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    7,606
    Right, but what's calling these methods? And why aren't they just passing the necessary information through instead of it having to be found every time?
     
    JustAsking06 likes this.
  8. JustAsking06

    JustAsking06

    Joined:
    Aug 11, 2021
    Posts:
    38
    Because the price will change after purchage like if you bought for 1000money and then the new prıce will be like 3000...

    Or is there a better way that im missing to pass that informations
     
  9. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    7,606
    Answer the question... what's calling this method?
     
    JustAsking06 likes this.
  10. JustAsking06

    JustAsking06

    Joined:
    Aug 11, 2021
    Posts:
    38
    Ohh sorry i understand now. I have trouble understanding sometimes because my English is not very good.
    When I understood the question, I think I understood the problem.

    ShopManager script calling this methods which is not necessary because i can set method to button so instead of calling them from another script i will assign that methods to buttons Did i understand true?
     
  11. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    7,606
    It would make more sense if each of the buttons OnClick event just referenced a method on their own
    ButtonInfo
    component, rather than a method of the manager.

    Right now you have half the work in the manager, half the work in the button. Keep it all in the button and its components.

    Same with the
    GunUseButton
    .
     
    JustAsking06 likes this.
  12. JustAsking06

    JustAsking06

    Joined:
    Aug 11, 2021
    Posts:
    38
    Thanks to you, I understood, so I don't need to use getcompenent every time unnecessarily.

    Thanks to your suggestions in the previous thread and these suggestions, my code has become much better. Thanks a lot
     
  13. JustAsking06

    JustAsking06

    Joined:
    Aug 11, 2021
    Posts:
    38
    Hey so i found one problem in that way.
    If i stop using shopmanager and referance buttoninfo to onclick event then i will need to set gamemanager to multiple buttoninfo scripts which will be a lot of getcompenent
     
  14. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    7,606
    Your game manager will probably benefit from expressing a singleton pattern instances. If you don't know how to do that, there's a million tutorials out there when it comes to setting up a singleton.
     
    JustAsking06 likes this.
  15. JustAsking06

    JustAsking06

    Joined:
    Aug 11, 2021
    Posts:
    38
    I will check it, thanks again.