- Home /
How do I Instantiate a prefab in a list?
Hi, i'm a super newbie, I'm trying to piece together a loan amortization calculator, I got the calculator working but now I want to populate the amortization table. I've tried populating a List by instantiating a prefab in a horizontal layout group, but I'm failing to get it to work in the loop. Here is the code:
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine.UI;
 using UnityEngine;
 using System;
 
 public class CalculatorButtonScript : MonoBehaviour
 {
 
     [SerializeField]
     private GameObject tableTemplate;
     
     public Button theButton;
 
     public InputField lnamt, intrate, _startDate, reperiod, mnthlypay, _rowNo, _date, _payment, _interest, _totalRepayment, _balancePrincipal, _balanceInterest, _total;
     // public Text 
 
     private List<GameObject> buttons;
 
     private void Thedate()
     {
 
         _startDate.text = DatePickerControl.dateStringFormato;
     }
 
 
     // Use this for initialization
     void Start()
     {
         theButton = GetComponent<Button>();
         theButton.onClick.AddListener(doTheMath);
 
 
     }
 
 
     void doTheMath()
     {
         
         Thedate();
         DateTime payDate = DateTime.ParseExact(_startDate.text, "yyyyMMdd", null);
         double interestRate = 0;
         double monthlyInterest = 0;
         double loanAmount;
         double amortizationTerm = 0;
         double currentBalance;
         double cummulativeInterest = 0;
         double monthlyPrincipal = 0;
         double cummulativePrincipal = 0;
 
         String imonthlyPayment, imonthlyInterest, icurrentBalance, icummulativeInterest;
 
         loanAmount = double.Parse(lnamt.text);
         currentBalance = loanAmount;
         interestRate = double.Parse(intrate.text) * 0.01;
         amortizationTerm = double.Parse(reperiod.text) * 12;
 
 
 
         // Calculate the monthly payment and round it to 2 decimal places           
         var monthlyPayment = ((interestRate / 12) / (1 - (Math.Pow((1 + (interestRate / 12)), -(amortizationTerm))))) * loanAmount;
         monthlyPayment = Math.Round(monthlyPayment, 2);
 
         imonthlyPayment = Convert.ToString(monthlyPayment);
         imonthlyPayment = String.Format("{0:C}", monthlyPayment);
 
         mnthlypay.text = (imonthlyPayment);
 
         // Storage List
 
         amortPaymentList = new List<GameObject>();
 
         // Loop for amortization term (number of monthly payments)
         for (int j = 0; j < amortizationTerm; j++)
         {
             // Calculate monthly cycle
             monthlyInterest = currentBalance * interestRate / 12; 
             monthlyPrincipal = monthlyPayment - monthlyInterest;
             currentBalance = currentBalance - monthlyPrincipal;
 
 
 
             if (j == amortizationTerm - 1 && currentBalance != monthlyPayment)
             {
                 // Adjust the last payment to make sure the final balance is 0
                 monthlyPayment += currentBalance;
                 currentBalance = 0;
             }
 
             // Reset Date
             payDate = payDate.AddMonths(1);
             // Add to cummulative totals
             cummulativeInterest += monthlyInterest;
             cummulativePrincipal += monthlyPrincipal;
 
             
             // This is the part where I'm totally lost
 
             if (amortPaymentList.Count > 0)
             {
                 foreach (GameObject tableTemplate in amortPaymentLists) //< ----- I have a prebab named 'Table' which I dropped in the 'tableTemplate' [SerialiseFiled]
                 {
                     amortPaymentList(tableTemplate.gameObject);
                 }
 
                 buttons.Clear();
             }
 
             foreach () // < ----- Not sure what to put here
             {
                 GameObject button = Instantiate(tableTamplate) as GameObject;
                 button.SetActive(true);
 
                 button.transform.SetParent(tableTemplate.transform.parent, false);
             }
 
 
             // Add values to Amortization Table     
 
             imonthlyInterest = Convert.ToString(monthlyInterest);
             imonthlyInterest = String.Format("{0:C}", monthlyInterest);
 
             icurrentBalance = Convert.ToString(currentBalance);
             icurrentBalance = String.Format("{0:C}", currentBalance);
 
             icummulativeInterest = Convert.ToString(cummulativeInterest);
             icummulativeInterest = String.Format("{0:C}", cummulativeInterest);
 
 
 
             _rowNo.text = (j + 1).ToString();
             _date.text = payDate.ToString("yyyyMMdd");
             _payment.text = (imonthlyPayment);
             _interest.text = (imonthlyInterest);
             _totalRepayment.text = (imonthlyPayment) + (imonthlyInterest);
             _balancePrincipal.text = (icurrentBalance);
             _balanceInterest.text = (icummulativeInterest);
             _total.text = (icurrentBalance) + (icummulativeInterest);
 
         }
     }
 }
               Comment
              
 
               
              Your answer
 
 
             Follow this Question
Related Questions
OverlapSphere for parallel arrays 1 Answer
Instantiate prefab once help C# 1 Answer
How to make TCG Deck (Instatiate based on Prefab) 1 Answer
Issue Instantiating prefab in C# 0 Answers
How to break a prefab connection via c# 0 Answers
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                