So, I’m getting an extra ‘0’ debug value somehow. It seems to be something to do with the ‘CalculatePercentageDeductions’ function. If I bypass it, I seem to get a single log, but after multiplying it in the function, I get the extra result. Also, I’ve done a Cntr + f to look for any active log statements that I might have missed, but I’m not seeing anything.
Sorry for the convoluted code. I’m newer to this, so I know that I have extraneous stuff going on. But the short version is that the script at the bottom “Revenue Sources” takes an input from a field, converts it to a percentage, and assigns it to the variable ‘percentage’ (ln 24.) The upper script, “GameObjectInsta” has the function ‘CalculatePercentageDeductions’ (ln 149), which on mouse up (ln 71) takes that variable, ‘percentage’, and then multiplies it by the function, ‘AddTotalGrossPay’.
If, for example, I put a 5 into the percent field, and 100 under the gross pay, I correctly get a 5 (.05 * 100), but I also get an additional 0. If I then add something to the deductions (to still make the gross equal 100), I get 5, 0, 0.
So, I’m guessing that there is an issue going on that I’m not aware of going on with the foreach commands in the different functions. Another set of eyes would be very appreciated, thank you!
public class GameObjectInsta : MonoBehaviour
{
public List<Employees> fields;
public GameObject parentOfField;
public Employees fieldToCreate;
public bool buttonWasPressed = false;
public float totalHours;
public TMP_Text outputHoursField;//bottom right, gives total
string totalHoursString;
int numbEmps;
public float hourlyRate;
public TMP_Text outputHourlyRateField;
public float totalGrossPay;
public float payEach;
float iPayEach;
// public TMP_InputField finalHoursField;
// Gross Pay Variables
public List<RevenueSources> revenueSourcesList;
public GameObject parentOfRevenuePayField;
public RevenueSources RevenuePrefabToCreate;
public TMP_Text outputTotalRevenueField;
public float totalRevenue;
//Flat Deduction Variables
public GameObject parentOfFlatDeductionsField;
public RevenueSources flatDeductionsToCreate;
public TMP_Text outputTotalFlatDeductionsField;
public float totalFlatDeductions;
//Percentage Deduction Variables
public GameObject parentOfPercentDeductionsField;
public RevenueSources percentDeductionsToCreate;
// public TMP_Text outputTotalPercentDeductionsField;
public float totalPercentDeductions;
public float iPercentPayEach;
public void Start()
{
}
public void MakeField()
{
Employees fieldClone = Instantiate(fieldToCreate);
fieldClone.transform.SetParent(parentOfField.transform, false);
fields.Add(fieldClone);
//finalHoursField = fieldClone.VisiblePayField;
}
public void MakeRevenuePayField()
{
RevenueSources fieldCloneRevSrcs = Instantiate(RevenuePrefabToCreate);
fieldCloneRevSrcs.transform.SetParent(parentOfRevenuePayField.transform, false);
revenueSourcesList.Add(fieldCloneRevSrcs);
}
public void MakeFlatDeductionsField()
{
RevenueSources fieldCloneFltDed = Instantiate(flatDeductionsToCreate);
fieldCloneFltDed.transform.SetParent(parentOfFlatDeductionsField.transform, false);
revenueSourcesList.Add(fieldCloneFltDed);
}
public void MakePercentageDeductionsField()
{
RevenueSources fieldClonePrctDed = Instantiate(percentDeductionsToCreate);
fieldClonePrctDed.transform.SetParent(parentOfPercentDeductionsField.transform, false);
revenueSourcesList.Add(fieldClonePrctDed);
}
public void OnMouseUp()
{
buttonWasPressed = true;
AddTotalHours();
CalculatePayEach();
GetHourlyRate();
CalculatePercentageDeductions();
}
float AddTotalHours()
{
totalHours = 0;
if (buttonWasPressed == true)
{
foreach (Employees e in fields)
totalHours += e.hours;
outputHoursField.text = totalHours.ToString();
}
// buttonWasPressed = false;
return totalHours;
}
public float GetHourlyRate()
{
hourlyRate = totalGrossPay / totalHours;
outputHourlyRateField.text = hourlyRate.ToString("f2") + " p/h";
return hourlyRate;
}
public void CalculatePayEach()
{// doesn't look like a need for a button press indicator
// do i need to go ahead and assign it to the list within the loop?
foreach (Employees e in fields)
{
iPayEach = GetHourlyRate() * e.hours;
e.pay = iPayEach.ToString("f2");
// e.pay = iPayEach; put pay back to float to work
// finalHoursField.text = e.pay.ToString();
}
/*foreach (Employees employees in fields)
{
Debug.Log(employees.name);
Debug.Log(employees.hours);
Debug.Log(employees.pay);
}*/
}
public float AddTotalRevenue()
{
totalRevenue = 0;
{
foreach (RevenueSources r in revenueSourcesList)
totalRevenue += r.revenueAmount;
outputTotalRevenueField.text = totalRevenue.ToString();
//Debug.Log(totalRevenue);
}
// buttonWasPressed = false;
return totalRevenue;
}
public float AddTotalGrossPay()
{
totalGrossPay = (totalRevenue - totalFlatDeductions);
return totalGrossPay;
}
public float AddTotalFlatDeductions()
{
totalFlatDeductions = 0;
{
foreach (RevenueSources r in revenueSourcesList)
totalFlatDeductions += r.flatDeductionsAmount;
outputTotalFlatDeductionsField.text = totalFlatDeductions.ToString();
// Debug.Log(totalFlatDeductions);
}
// buttonWasPressed = false;
return totalFlatDeductions;
}
public void CalculatePercentageDeductions()
{
foreach (RevenueSources p in revenueSourcesList)
{
iPercentPayEach = p.percent * AddTotalGrossPay();//currently, post flat deductions
// finalHoursField.text = e.pay.ToString();
Debug.Log(iPercentPayEach);
}
}
private void Update()
{
AddTotalRevenue();
AddTotalFlatDeductions();
AddTotalGrossPay();
numbEmps = fields.Count;
}
}
public class RevenueSources : MonoBehaviour
//This is the class, pulling in the 'percentage' variable
{
public string revenueName;
public float revenueAmount;
public float flatDeductionsAmount;
public float percent;// actual percentage inputed
public string percentDeductionsAmount;//after calculation amount (to be displayed)
public TMP_InputField visiblePercentageDeductionsField;//
public void RevenueAmountAssigner(string revenueField)
{
float x = float.Parse(revenueField);
this.revenueAmount = x;
}
public void FlatDeductionsAmountAssigner(string flatDeductionsField)
{
float x = float.Parse(flatDeductionsField);
this.flatDeductionsAmount = x;
}
public void PercentAssigner(string percentage)
{
float x = float.Parse(percentage) * .01f;
{ this.percent = x; }
}
public void prnt()
{
// Debug.Log(revenueAmount);
}
void Update() {
// Debug.Log(percent);
}
}