- Home /
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?
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!
Your answer
Follow this Question
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