Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
2
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
Add comment
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

· Add your reply
  • Sort: 
avatar image
5
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
Add comment · 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, $$anonymous$$ike.

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 I$$anonymous$$O $$anonymous$$ike 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.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Trying to Highlight text in order to copy and paste, but do not want the text to be editable. 1 Answer

Changing the Standalone Input Manager variables 2 Answers

Get the color under the mouse cursor 2 Answers

Why won't my object activate? 0 Answers

Impossible to write in GUI input field 0 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