- Home /
How do I add/multiply input field boxes?
I am creating an app that needs to take the variable input from one input field UI box, then take the variable from the other box, and multiply them together to create an answer that shows up on the screen when a button is clicked.
I am unsure of the proper calling for the functions because when i look at the input field functions this website comes up: http://docs.unity3d.com/ScriptReference/UI.InputField.html It has everything i need except i cant find basic functions such as addition and multiplication as well as how to call an input field to be used.
If you have some sample code of how to do this, it would be much appreciated. JS is preferred for me but i know C# as well.
If you have any questions please feel free to ask. I may have not made my question clear enough.
Answer by DiegoSLTS · Mar 27, 2015 at 03:28 AM
InputFields contain a string text even if you set it to only admit numbers, so you have to start from there.
Also, InputFields are just that, a field where you can input something, adding and multiplying it's content is not part of their functionality.
To do math operations you have to take the text string from both InputFields (the "text" property) then parse an integer or decimal value from it (https://msdn.microsoft.com/en-us/library/vstudio/bb397679%28v=vs.110%29.aspx) and then make the operation with the common math operators.
Once you have the result you have to turn the number value into a string again (the ToString method) and set it as the "text" property of the Text component where you want to display the result.
A quick sample code, I haven't tested it:
using UnityEngine;
using UnityEngine.UI;
using System;
public class InputFieldOperations : MonoBehaviour {
public InputField Field1;
public InputField Field2;
public Text Result;
public void Sum() {
int a = Convert.ToInt32(Field1.text);
int b = Convert.ToInt32(Field2.text);
int c = a+b;
Result.text = c.ToString();
}
public void Product() {
int a = Convert.ToInt32(Field1.text);
int b = Convert.ToInt32(Field2.text);
int c = a*b;
Result.text = c.ToString();
}
}
Set that as a component of something, drag and drop the inputfields and the text and set the OnClick callback of a button to call the Sum method, and the OnClick callback of another button to call the Product method.
Would you recommend i place the main script like this on an empty game object with the canvas/all ui inside? or just place it on the canvas?
I decided to place it on an empty game object. 1 last thing. Unity doesnt recognize "Convert" in the context used and should i rename "Field1" and "Field2" to my own text box names so it will call from them?
I just tested that code and it's working for me. I can't recomend where to put that script, I added it to the Canvas for a quick test but I guess it would be better in an empty game object like you did or in a panel that contains evenrything. Or maybe it shouldn't be a component on it's own and should ins$$anonymous$$d be part of another bigger script that does other stuff.
Actually, I just wanted to give you a $$anonymous$$imum example of how to do operations between InputFields. You might even put that script on the Text that shows the result, make the Result reference private and the get Text component from the game object ins$$anonymous$$d.
About rena$$anonymous$$g variables, yes, name them however you think will look cleaner in your code. $$anonymous$$aybe "LeftOperand" and "RightOperand" or something less abstract if your problem has a name for what those numbers mean.
I have a question about the 'Result.text = c.ToString();'
Does this make it so that as soon as the values are put into the boxes it automatically updates? I am using a button to calculate the two variables once they are put into the boxes. How would i implement that? Thank you so much this far. Im sorry im pretty new to the UI.
InputFields have 2 events that are triggered automatically, one is "On value change" that's triggered every time you press a key, and the other is "End Edit", triggered when you press enter or click outside the InputField.
You can setup callbacks to be called in those events, just like you do for buttons.
If you set a same function on the "On value change" of both InputFields that code will be executed automatically whenever you write some value. Note it will also execute when you erase values, so you have to add some checks or you might have some exceptions trying to convert an empty string into a value.
Your answer
Follow this Question
Related Questions
UI InputField text sometimes invisible? 1 Answer
How to change scoreInputField to get a score from another script/Scene 0 Answers
Showing UI image when inside a trigger and then pressing a button 2 Answers
Best way to show variable values on UI 1 Answer
Why does the keyboard control what button is selected? 2 Answers