L
L
laykketyt2021-01-14 21:04:03
C++ / C#
laykketyt, 2021-01-14 21:04:03

How to get int from inputField?

how to get int from inputField ?

Answer the question

In order to leave comments, you need to log in

4 answer(s)
V
Vladimir Korotenko, 2021-01-14
@firedragon

Forbid to enter everything except numbers? As a result, there will be a number

F
freeExec, 2021-01-14
@freeExec

If you enter a crap there, then no way. And if the correct number, then as you have in the previous question.

V
Vasily Bannikov, 2021-01-14
@vabka

InputField input = ...;
int number = int.Parse(input.text); // если в input.text будет лежать не число, то будет FormatException

or
InputField input = ...;
if(int.TryParse(input.text, out var number) {
  // используем number. В этом случае FormatException не выпадет, но тогда будет возникать ощущение, будто ничего не работает.
}

C
CHIDWI, 2021-01-15
@CHIDWI

In the inspector, you set the Input Field element itself to Integer (then you can enter only integers in the field), the output will still be a string and then you will convert it to int.

InputField numberInputField = ...; //тут подключаешь своё поле с которого читаешь
int number = int.Parse(numberInputField.text); //записываешь значение в переменную

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question