Help with using UI elements to populate a List using a custom class.

I’m have trouble with working out how to take the UI elements info and add it to a c# List.

I’ve created three scripts:

  • Bank - Used to set variables (i.e UI elements, ref to save prep script etc) and methods used.

  • BankSav - Has a list of account info to be saved. It uses the AccountInfo script as the list data type.

  • AccountInfo - Template of what account info needs to have.

Everything works if I populate the List using the Inspector but, I don’t know how to take the info from the UI and add it to the list.

I suspect that I need to convert the UI info into AccountInfo data type which, I don’t know how to do.

Below are the scripts.

Bank

public class Bank : MonoBehaviour
{
    public enum Type { Loan, TermDeposit }
    public enum Term { Days7, Days14, Days30 }

    [Header("UI")]
    [SerializeField]
    private SaveManager saveManager;
    [SerializeField]
    public Dropdown accountType;
    [SerializeField]
    public InputField naneField;
    [SerializeField]
    public InputField descriptionField;
    [SerializeField]
    public InputField amountField;
    [SerializeField]
    public Dropdown termLength;
    [SerializeField]
    public Text baseInterestRate;
    [SerializeField]
    public Text bonusInterestRate;
    [SerializeField]
    public Text totalInterestRate;

    [Header("Acount Info")]
    [SerializeField]
    public List<AccountInfo> accountsInfo = new List<AccountInfo>();

    [Header("Acoount Info")]
    public BankSav bankSav;
       
    private Type AccountType;
    private Term TermLength;

    private void Update()
    {
        bankSav.accountsInfo = accountsInfo;
    }

    public void NewAccount()
    {
        if (accountType.value == 0)
        {
            Debug.LogWarning("Please select an account type.");
        }
        else
        {
            if (accountType.value == 1)
            {
                AccountType = Type.Loan;
            }
            else if (accountType.value == 2)
            {
                AccountType = Type.TermDeposit;
            }
        }

        if (accountType.value == 0)
        {
            Debug.LogWarning("Please select an account type.");
        }
        else
        {
            if (accountType.value == 1)
            {
                TermLength = Term.Days7;
            }
            else if (accountType.value == 2)
            {
                TermLength = Term.Days14;
            }
            else if (accountType.value == 3)
            {
                TermLength = Term.Days30;
            }
        }

        //Debug.Log("Account type: " + AccountType + " Account name: " + naneField.text + " Account description: "
        //        + descriptionField.text + " Account amount: " + amountField.text + " Account term: " + TermLength);
    }

    public void NewBalance()
    {
           
    }

    public void Save()
    {
        saveManager.Save();
    }

    public void Load()
    {
        saveManager.Load();
    }
}

BankSav

using System.Collections.Generic;

[System.Serializable]
    public class BankSav
    {
        public List<AccountInfo> accountsInfo = new List<AccountInfo>();
    }

AccountInfo

using static Bank;

[System.Serializable]
    public class AccountInfo
    {
        public int accountID;
        public Type type;
        public string name;
        public string description;
        public int balance;
        public Term term;
    }
public enum Type
{
    None = 0,
    Loan,
    TermDeposit,
    ThirdAccountType
}

public void NewAccount()
{
    if (accountType.value == Type.None.ToString())
    {
        Debug.LogWarning("Please select an account type.");
    }
    etc..
}

I see that you’re checking 2 times

if (accountType.value == 0)
if (accountType.value == 1)
if (accountType.value == 2)
//try to compress your code.

I noticed that after posting the above code and fixed it. Thnks thou

I found this link on google.

Used the

parts.Add(new Part { PartName = "crank arm", PartId = 1234 });

bit from it (made the changes needed for my script) and I got it to work.