Question by
unity_10783358 · Oct 26, 2020 at 11:22 AM ·
beginnercalculationcalculationsbeginners
URGENT HELP How to add "+,-,x, / and =" to my 3D Calculator
Hi I'm making a 3d calculator that you can jump on each individual number for it to show up on the screen. but it only shows one number at a time and doesn't do the calculators basic functions such as addition, subtraction and also working out the total could someone help me on what i need to add to get this to work. Script i have so far:
public float firstNumber = 0f, secondNumber = 0f;
float product = 0f;
public TextMeshProUGUI text;
string op;
public void Value(float value)
{
if (op == null)//** Let's us continue
{
firstNumber = value;
text.text = firstNumber.ToString() + " ";
}
else
{
secondNumber = value;
text.text += secondNumber.ToString() + " ";
}
}
//this means the script needs to be on the player
void OnTriggerEnter(Collider other)
{
if ( other.gameObject.CompareTag("button"))
{
Debug.Log(firstNumber.ToString());
Value(float.Parse(other.gameObject.name));
}
}
public void Operation(string operation)
{
if (operation == "+")
{
op = "+ ";
}
else if (operation == "-")
{
op = "- ";
}
else if (operation == "=")
{
Calculate();
}
text.text += op;
}
void Calculate()
{
if (op == "+ ")
{
product = firstNumber + secondNumber;
}
else if (op == "- ")
{
product = firstNumber - secondNumber;
}
text.text += "= " + product.ToString();
//Resets
op = null;
firstNumber = product;
secondNumber = 0;
}
}
Comment