Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Update array of UI elements with data from other array.

Discussion in 'Scripting' started by zurisar, Dec 4, 2020.

  1. zurisar

    zurisar

    Joined:
    Nov 4, 2019
    Posts:
    24
    Hi folks! I still learning Unity and C#, and want to ask, does I do this thing right. In my project I want to update few UI elements from script at one time, this is list of weapons with text field with ammo qty for each weapon, player see all list everytime, so I use few arrays.
    One array from my WeaponController script, this array contain list of weapons. Second array from my UIController script, this array contain text fields, for UI update I use events, I update UI only when player fire or pickup ammo.

    So question is: does my UI update function right?
    Code (CSharp):
    1. void UpdateAmmoText()
    2.     {
    3.         string text = "0";
    4.        
    5.         foreach(Weapon weapon in WeaponController.Instance.weapons)
    6.         {
    7.             int ammo = weapon.weaponAmmo;
    8.             string weaponName = weapon.weaponType.ToString();
    9.             if (ammo == -1) { text = "Max"; }
    10.             else text = ammo.ToString();
    11.             foreach (GameObject textField in WeaponAmmoText)
    12.             {
    13.                 if (textField.name.Contains(weaponName))
    14.                 {
    15.                     textField.GetComponent<Text>().text = text;
    16.                 }
    17.             }
    18.         }
    19.     }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,762
    I can't see your screen. Does it update right?
     
  3. zurisar

    zurisar

    Joined:
    Nov 4, 2019
    Posts:
    24
    Yep, all looks fine, I ask mostly about code architecture.
    Screenshot_2020-12-05-00-46-33-000_ru.zapps.zurisar.AsteroidsTerror.jpg
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,762
    Well that's kinda personal, but I would NEVER make an if/else statement the way you did above.

    Always

    Code (csharp):
    1. if(condition
    2. {
    3.  thing();
    4. }
    5. else
    6. {
    7.  thang();
    8. }
    I would also move line 3 inside the loop, since you've used it as a default, but it only applies to the first. If you were to change your if logic, there's a chance item X would be redisplayed in item X+1...

    just minor stuff that has bitten me many times in the past.
     
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,762
    Also, you're using some weird look-for-name function with the contains thing... Make a direct link to what things need to display and/or connect themselves to their display methods. Otherwise if you ever have a duplicate-named weapon (even inadvertently), a bunch of weird bad stuff will happen.

    For instance, you might have a "Sword" and a "Scimitar" and then when you translate your game to native Elbonian language, they use the same name for "Sword" and "Scimitar" and now your code breaks.