What I want: In a cost calculator project, I need to have a field with a calculated result that is able to be overridden and recalculated by user input, i.e., the calculation is a suggested number of units to purchase based on the calculation, however, the user should be able to override that with their own number and be reflected in the calculation of TotalRacks().
What I’ve tried: My current approach has been to use an Input Field and have the placeholder text be the result of the calculation. This works fine on its own (the calculation shows up correctly). The problem is when I manually input a number in that field and attempt to recalculate (I have a “Calculate” button in the UI), the result doesn’t change.
I don’t if my approach is on track but I’m missing something or if I’m completely off base with my line of thought. Any help/suggestions/solutions please? I’ve included my code (sorry if it’s messy or no optimal, I’m relatively new to programming) and a screenshot of the project, but please let me know if more information is needed.
using UnityEngine;
using UnityEngine.UI;
using System;
public class InputSFCalc : MonoBehaviour
{
public InputField Field1;
public InputField Field2;
public InputField Field3;
public InputField CostPerRack;
public InputField RacksSuggested;
public Text sfResult;
public Text rollsCost;
public Text rollsNeeded;
public Text velcroLength;
public Text velcroFree;
public Text racksTotal;
int rollWidth = 6;
int rollDivider = 75;
int rackRollDivider = 7000;
bool rack;
public void Start()
{
RacksSuggested.onValueChanged.AddListener(delegate { RackValueChange(); });
}
public void Update()
{
}
public void Sum()
{
int a = Convert.ToInt32(Field1.text);
int b = Convert.ToInt32(Field2.text);
int c = a + b;
sfResult.text = c.ToString();
}
public void Product()
{
int a = Convert.ToInt32(Field1.text);
int b = Convert.ToInt32(Field2.text);
int c = a * b;
sfResult.text = c.ToString("n0");
}
public void RollsCost()
{
decimal a = Convert.ToDecimal(Field3.text);
decimal b = Convert.ToDecimal(sfResult.text);
decimal c = a * b;
rollsCost.text = c.ToString("C");
}
public void RollsNeeded()
{
int a = Convert.ToInt32(Field2.text);
int c = a / rollWidth;
rollsNeeded.text = c.ToString();
}
public void VelcroLength()
{
int a = Convert.ToInt32(rollsNeeded.text);
int b = Convert.ToInt32(Field1.text);
int c = (a * b) - b;
velcroLength.text = c.ToString("n0") + " ft";
}
public void VelcroFree()
{
double a = Convert.ToInt64(rollsNeeded.text);
double b = Convert.ToInt64(Field1.text);
double c = (a * b) - b;
double d = c / rollDivider;
double e = Math.Ceiling(d);
velcroFree.text = e.ToString();
}
public void RackValueChange()
{
rack = RacksSuggested.GetComponent<InputField>().placeholder.GetComponent<Text>().text == null;
Debug.Log("Rack Value is Null");
}
public void RacksNeeded() //default value is 1
{
if (RacksSuggested.GetComponent<InputField>().placeholder.GetComponent<Text>().text == null)
{
RacksSuggested.text.ToString();
}
else
{
double a = Convert.ToInt32(Field1.text);
double b = Convert.ToInt32(Field2.text);
double c = a * b;
double d = c / rackRollDivider;
double e = Math.Ceiling(d);
RacksSuggested.GetComponent<InputField>().placeholder.GetComponent<Text>().text = e.ToString();
}
}
public void RacksTotal()
{
if(RacksSuggested.GetComponent<InputField>().placeholder.GetComponent<Text>().text != null)
{
decimal w = Convert.ToInt32(Field1.text);
decimal x = Convert.ToInt32(Field2.text);
decimal y = w * x;
decimal z = y / rackRollDivider;
decimal v = Math.Ceiling(z);
decimal b = v;
decimal a = Convert.ToDecimal(CostPerRack.text);
//decimal b = Convert.ToDecimal(RacksSuggested.text);
decimal c = a * b;
racksTotal.text = c.ToString("C");
}
else
{
decimal a = Convert.ToDecimal(CostPerRack.text);
decimal b = Convert.ToDecimal(RacksSuggested.text);
decimal c = a * b;
racksTotal.text = c.ToString("C");
}
}
}