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
0
Question by Blueskai · Mar 24, 2015 at 06:27 AM · uiinputfield

Saving only Integers in an inputfield via code

I need to save integers in a variable through inputfield but i dont know how and i have searched all over the internet but i cant find any solution to this problem here is an example of what i mean

public var number : int; public var inputField : InputField;

function Start() { inputField = GetComponent(InputField); }

function Update() { number = inputField.text; //i cant do that because its not possible to save string into an int variable but thats basically what i mean.. saving Integers into an int vriable from an InputField...

i have no clue how to do.. would like to get some help from you guys :)

}

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
0

Answer by siaran · Mar 24, 2015 at 01:43 PM

You need to parse your string to an int. Lucky for you, you probably won´t have to write code to do that all yourself, here is a method I found for javascript:

http://www.w3schools.com/jsref/jsref_parseint.asp

Comment
Add comment · Show 3 · 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 Blueskai · Mar 24, 2015 at 02:07 PM 0
Share

i want pepole to input only numbers into an inputField box and then it will save the number they put in an int variable.

will this function help me to do that?

avatar image siaran · Mar 24, 2015 at 03:05 PM 0
Share

...it converts a string (what you get in your input) to an int (what you want). If you want to make sure people only put ints in your input field, you'll have to check if what they give is an int. parseInt returns NaN when the first character in the string is not an int, so you could write something like

 //store field input
 string fieldInput;
 //int
 int someInt;
 
 fieldInput = inputField.text;
 
 if(!isNaN(parseInt(fieldInput)){
  someInt = parseInt(fieldInput);
  //do stuff with someInt
 }else{
  //do something to tell user that his input is invalid
 }


avatar image DiegoSLTS · Mar 24, 2015 at 03:48 PM 0
Share

You can setup your InputField component to only accept numbers, you don't need to check that in code. Look at the "Content type" property of the InputField, one of the options is "Integer number".

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Masked InputField on Unity UI 4 Answers

How can I block Copy Paste from Input Field? 3 Answers

Input fields act abritrarily 0 Answers

Android Keyboard Stuck / Won't Hide? 1 Answer

Auto layout and content fitter with InputField 1 Answer


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