• 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 /
avatar image
Question by dot · Dec 28, 2010 at 12:42 PM · guiinputnumbers

text field - inputting numbers only

hey I am looking for a way to input numbers to a text field - or any other way do the following:

there's a place where a player can click and input a number. from between 0-300

the field will obviously have to ignore any letters entered.

Comment
sdgd
SepM

People who like this

2 Show 0
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

1 Reply

  • Sort: 
avatar image
Best Answer

Answer by Mike 3 · Dec 28, 2010 at 01:18 PM

Off the top of my head, something like this should do it, where you could change int to float all three times if you didn't want integer values:

var num : int = 0;

function OnGUI() { var text = GUI.TextField(Rect(50, 50, 200, 50), num.ToString()); var temp : int = 0; if (int.TryParse(text, temp)) { num = Mathf.Clamp(0, temp); } else if (text == "") num = 0; }

Comment
dot
Eric5h5
sdgd
Nition
ExtremePowers
chrizztineyu
SepM

People who like this

5 Show 5 · 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 dot · Dec 28, 2010 at 02:18 PM 0
Share

wow. brief and clever. thank you, Mike.

avatar image judy3t · Apr 06, 2012 at 01:54 AM 0
Share

Thanks, that solves the multiple character issue. It still doesn't accept negative number strings but I have an ugly hacked work around for that for now. What I don't understand is where I have indicated in any way that this is an int only text field. Why doesn't it accept alphas?

avatar image Eric5h5 · Apr 06, 2012 at 02:30 AM 0
Share

That code won't actually work (aside from the syntax error). The text variable will always be "0" and the textfield won't accept any input, since it's just getting the value from num.ToString, which logically can't change since there's no way for the user to actually input any text. It's better to block unwanted characters entirely; see my answer here.

avatar image sdgd · Mar 18, 2013 at 11:02 PM 0
Share

many times I get to this Q when I searching this but right now I've managed to figure out something more:

             GUILayout.BeginHorizontal();
             GUILayout.Label("size of Hall " + somefloat);
             somefloat = GUILayout.HorizontalScrollbar((int)somefloat,0.01f, 0f, 300.1f,GUILayout.Width(50));
             GUILayout.EndHorizontal();

have fun coding ;)

it's int in the end ;)

avatar image Nition · Nov 07, 2013 at 01:03 AM 0
Share

@Eric5h5 IMO Mike 3's solution is better in this case because it guarantees that some stray forgotten character can't get through and create an invalid Int. Not sure how you were reading it because it does work; it accepts any input that can be parsed as an int and rejects all else.

Unity Answers is in Read-Only mode

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta by June 9. Please note, Unity Answers is now in read-only so we can prepare for the final data migration.

For more information and updates, please read our full announcement thread in the Unity Forum.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

In game proccess GUI clicks 1 Answer

Mac menu bar interfering with click events 0 Answers

How Can I Create A Basic Developer Console? 0 Answers

How to get print to transfer to a GUI Text? 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