How to get a number from a UI input field
Long story short, I have a UI input field that I have set to only accept decimal inputs. When I go to access this input through code its type is string. How can I make this input a float or int so I can use math functions with it (add, subtract, multiply, divide, ect.)
Ps. I use C# for programming.
Answer by SirPaul · Dec 14, 2016 at 01:43 PM
@Superspeedy Here's an example for you of how to retrieve data from UI inputfield;
InputField input;
int integer_Value_we_Want;
integer_Value_we_Want = int.Parse(input.text); //for integer
integer_Value_we_Want = float.Parse(input.text); //for float
integer_Value_we_Want = double.Parse(input.text); //for double
and so on..
@SirPaul, Thanks a lot! I was having the same problem and this really helped me out!
Thank you for a clear and simple answer. I helped a lot.
Something to note here. If you parse a float or a double and try store it in an int then you will get an error. You cannot implicitly convert floats and doubles into ints.
Answer by YEX_ · Jun 01, 2021 at 04:39 PM
int.Parse() Does'nt work any more, you have to use TryParse()
InputField input; int number; int.TryParse(input.text, out int result); number = result;
Your answer

Follow this Question
Related Questions
iOS TouchScreenKeyboard closes and open when tapping on another InputField 4 Answers
Disable inputField navigation(Navigation between text or caret navigation) 1 Answer
Keyboard not opening when I am trying to shift to next input field through script 0 Answers
Cannot clear text from inputfield 1 Answer