- Home /
How to insantiate an Inputfield which is populated with numerical values and to add the values of the field to get an output values?
I have instantiated a prefab Input Field and each input field has some Integer values, i'm unable to add the values of the instantiated input field to get an output!!!!!!!!!
Good day.
What you mean with "add values" ? add values where, inside the textbox? why you need to do this? an input is for players to type things..
And what you mean for get an output?
A textbox have the property text, which is a string variable, you can read it, acces and set it. I don't get what you mean by "output".
Please explain more..
Thanks! Bye!
Good day :D
i entered numerical values into the input fields and for adding used float.parse and then into string.
i understand even less than before.. are you sure you are using the correct names of the things?
"you" are entring numbers to an input textbox? ¿why? why dont just simple use a text? ¿during play? or the user will do it?
If I understand it correctly, you instantiated a number of InputField
s, then (in play mode) entered numerical values into them, and you want to turn get the values in them, add them up, and return an output (possibly in string).
For this, you need to get the content of each InputField
from InputField.text
, parse the value with float.Parse()
, store the result in a float
, add the values. float
s usually automatically convert to string
, but that doesn't happen, you can use ToString()
for that.
I see that you already mentioned doing this, but didn't work. What is the problem?
Some code:
List<InputField> inputFields; // I assume you have your InputFields in this list
Text outputLabel; // I assume you want to output the sum onto a Text, but the code below would be the same for an InputField
float sum = 0;
for (int i = 0; i < inputFields.Count; ++i) {
float value;
if (float.TryParse(inputFields[i].text, out value)) { sum += value; }
else { Debug.LogError("Content of InputField " + i + " is not a valid number!"); }
}
outputLabel.text = sum.ToString();
Your answer
Follow this Question
Related Questions
Request feedback for C# script (instantiating UI elements depending on Player Input) 0 Answers
Shouldn't taps on UI.InputField open Keyboard on mobile ? 1 Answer
Mouse Methods (OnMouseDown, OnMouseOver etc) stop working after certain circumstances 0 Answers