Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
1
Question by Core · May 30, 2011 at 06:26 PM · iosstringkeyboardconvertparsing

iOS Not Converting String to Integer

The following code works fine in Unity PC but not when I run the app. on the iPad. I validate keyboard input using the following C# script (my thanks to Sbd on the Unity Forum) to ensure only numbers are entered.

This script checks the user input to ensure that only natural numbers are entered. It passes the user input to the public variable "userInputUtensil" if the validation test is passed

 using UnityEngine;
 using System.Text.RegularExpressions;
 
 public class ValidateInputTestUtensil: MonoBehaviour 
 {
  Regex numberRegex = new Regex("0*[0-9][1-9]*");//validates user input to ensure that only numbers are entered
  Rect numberPosition = new Rect (260, 570, 30, 20);
  string numberText = "";
  public string userInputUtensil;
  
  void Update () {//Use the Back Space key
  foreach (char c in Input.inputString) {
         // Backspace - Remove the last character
         if (c == '\b') {
             if (numberText.Length != 0)
                 numberText = numberText.Substring(0, numberText.Length - 1);
                   userInputUtensil = numberText;
         }
      }
  }
 void OnGUI()
 {
 
  if (numberRegex.IsMatch(Input.inputString)){
   numberText = GUI.TextField(numberPosition, numberText, 3); //Last number indicates that only three numbers may be entered
   userInputUtensil = numberText;
  } else {
    GUI.TextField(numberPosition, numberText, 3);
        }
      }
   }

When I hit a button my Unity script grabs the input from the above script and parses it:

 GUI.color = new Color(1.0, 1.0, 1.0, MyGUIAlpha4);
   if(GUI.Button (Rect(20,555,135,141), controlTexture) ) {        
     userSizeUtensil = GetComponent("ValidateInputTestUtensil"); //Get the user input (userInputUtensil variable) from C# script "ValidateInputTestUtensil"
         sizeNew = userSizeUtensil.userInputUtensil;//Allocate the userInput string to the sizeNew variable
                 
                 if (sizeNew == ""){ //Check that the user has actually entered a number, if not pass the following message
                     MessageUtensil = "Please enter a number from 10 to 300";//pass this message to the "message" variable
                     } else {//If above condition complied with clear the error message or don't fire it in the first place                    
                         MessageUtensil = "";//Clear the message or no message        
                         }
                         
                         if (sizeNew != null){//Only if a number has been entered then proceed
                           SizeUtensil = int.Parse (sizeNew);//Parse the string to convert it to an integer
                             if ((SizeUtensil < 10) || (SizeUtensil > 300)){ //Check that the number is between 11 and 301 and not null                            
                             MessageUtensil = "Please enter a number from 10 to 300";//if null or not 10 to 300 pass this message to the "message" variable
                               } else {//If above condition complied with clear the error message or don't fire it in the first place
                                 MessageUtensil = "";//Clear the message or no message        
                                             }
                                         }
                                     }

This all works fine in Unity for PC but on the iPad i keep getting the message "Please enter a number from 10 to 300" even though I have entered a number in the text field. In addition the keyboard doesn't limit the input to just number but allows all other keyboard characters as well. Is there another way that I should be parsing strings and limiting keyboard input to numbers only?

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 Core · Jun 01, 2011 at 05:53 PM 0
Share

What? Nobody willing to share any thoughts on where I'm going wrong? I've just started coding for iPad so I'm really stuck here folks!

0 Replies

· Add your reply
  • Sort: 

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

1 Person is following this question.

avatar image

Related Questions

String Parsing 1 Answer

iOS keyboard - limit number of keyboard characters 0 Answers

How to convert a string to int array in Unity C# 1 Answer

Converting 1s and 0s to float 1 Answer

Mobile Virtual Joysticks / Keyboard Script Conversion 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