Allow only numbers to a textfield, and make it an int.

How do I use a textfield that can only have numbers typed into it? Also, I need all the things typed in to be converted from a string, to an int.

I want a user to be able to type a number between 1 and 70 into the textfield.
I will then feed that number into a loop which will determine how many times the prefab is instantiated.

I’ve searched many times (MANY) aand couldn’t find anything.

Thanks in advance!

Hi there

Its even simpler!

Assuming your using the new UI!

Just change your Inspector to Debug Mode and Set Integer or Float as the Validation type.


The string can be easily parsed to int by calling int.Parse(yourString). After you’ve passed it you could also check if its within your range.


In your OnGUI() code look at the string which is being entered into the TextField, and remove any characters that are not digits.

This has been answered many times; just doing a search for “textfield int” turned up a bunch of answers. Restrict characters in GUI.TextField - Unity Answers Use Int.Parse to convert a string to an int.