- Home /
Making a input field's value act like a int
Hi!
I need my input field to act like a int. I already got it working so it only accepts numbers but when I try and use it in code, it is like a string. I am using the new 4.6 beta UI. Do anyone know how I could do this?
Thanks
Not sure where you are having the problem. int.TryParse() will convert an integer to a string...and there are other methods.
Answer by Slev · Sep 20, 2014 at 06:04 PM
The input value of the InputField is always a string. All you need to do is use a cast. In C#"
int num_to_use = Convert.ToInt32(your_input.value);
It should be similar in JS.
@robertbu You are correct, let me fix that... slipped my $$anonymous$$d it was string -> int not int -> string
@Slev, it does not work either. A string is a string and a int is a int, they are not even the same type of data...
@fafase yes it does Convert.ToInt32(string s) takes a string and makes it a 32 bit integer.
Note that Convert.ToInt32() raises and exception if the string passed cannot be converted to an integer, so you would need to add a try/catch block if there is any chance that the string something other than a number (like an empty string). int.TryParse() returns a boolean on whether the conversion succeeded and passes back the value in an 'out' parameter...no need for a try/catch block.
Your answer
Follow this Question
Related Questions
Score text wont appear 1 Answer
Displaying variable on UI text every frame (JS) 1 Answer
Using Player Prefs for a Leaderboard 1 Answer
Add multiple ints to a string line. 2 Answers