Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This question was closed Jan 02, 2015 at 07:16 PM by Eric5h5 for the following reason:

Duplicate Question

avatar image
1
Question by The_Unity_Game_Developer · Jan 02, 2015 at 06:30 PM · textfieldhow-to

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!

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Qasem2014 · Jan 02, 2015 at 07:07 PM 0
Share

convert to int should be this :

 var intvariable : int = parseInt(stringvariable);

and for checking the characters i don't have a good idea but i think you need a loop like this :

 for (var i = 0; i < stringvariable.Length; i++)
 { }

then compare every character with numbers (a array of 0-9) . that should be another loop ;) i hope it helps you

3 Replies

  • Sort: 
avatar image
12

Answer by code_warrior · Jan 02, 2015 at 07:12 PM

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.

alt text

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.

code_warrior


validation.jpg (21.4 kB)
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image sonnyb · Jul 03, 2019 at 09:52 PM 0
Share

Similarly, InputField.ContentType can be set in the inspector in Normal $$anonymous$$ode. (I just tried on Unity 2018.3.)

https://docs.unity3d.com/ScriptReference/UI.InputField.ContentType.html

avatar image
0

Answer by Graham-Dunnett · Jan 02, 2015 at 06:31 PM

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

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image The_Unity_Game_Developer · Jan 02, 2015 at 06:41 PM 0
Share

How would I do that? Also, how can I convert the string content into an int? I need it to be compatible with a loop int.

avatar image
0

Answer by Eric5h5 · Jan 02, 2015 at 07:16 PM

This has been answered many times; just doing a search for "textfield int" turned up a bunch of answers. http://answers.unity3d.com/questions/18736/restrict-characters-in-guitextfield.html Use Int.Parse to convert a string to an int.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Follow this Question

Answers Answers and Comments

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

GUI.Textfield to int ? 2 Answers

TextField not clearing 3 Answers

Typing text and close the box 0 Answers

Allow only numbers in an input field 1 Answer

Input not registering while TextField is selected 3 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges