I have the attached script tied to a button that deletes user input one character at a time. I originally had this script written using Visual Studio. I changed the IDE over to Mono Develop and rewrote all of my scripts. In visual studio this script cleared the InputField when there were 0 characters. However, when I switched it to MonoDevelop I started to get this error when there is only 1 character left in the input field and I click the backspace button. The error is "FormatException: Input string was not in the correct format:. Any ideas?
Should also mention that even though the inputfield retains the last character it does not retain that information. For instance: If i had the word “start” in the field and deleted it completely the “s” would still be in the inputfield. However, as soon as i enter a different character it overrides the “s” with it.
I have continued to work on this problem with no success. Everything shoots out the FormatException. Sometimes it says that it cannot convert string other times it shows a Parse error.
I hope to release the app on iOS and was told that Mono is more compatible. I found a tutorial on making the button clear a field on click just using the inspector in Unity. When I switched to that it still showed the error and would not clear the field.
You can use VS just fine. At work, I may do the android build on VS and then when it’s passed to the iOS guy, he can build out without ever touching the code (though he does use mono). There is also a VS for mac and VS code that can run on mac.
You should have no issues using Visual Studios if you’re doing your builds on PC to begin with.
Honestly, I’m not seeing issues with what you posted, so something else has to be going on.
I for one am puzzled why you’re adding a backspace function to the input field. Does it not already handle that when you press backspace? Isn’t that pretty much what InputField does?
Let’s be clear on this: the script editor you use is literally a text editor as far as Unity is concerned. A very nice, code-focused text editor, but in this situation, that’s all it is. You could use Notepad to write your scripts if you wanted to. (There are some masochistic programmers who do just that.) The IDE you use for Unity will have no effect on how any code compiles, nor would changing to a different one ever require you to rewrite any scripts. And the IDE you use is not involved in the build process at all, either.
I am nearly certain that that error could not possibly be caused by the code you’ve posted. FormatException happens when you try to use a Parse function or use string.Format, and that code does neither of those. Can you post the full script and copy&paste the full error message?
I am making a phone app for children with a button pad built into the scene. This allows the user to enter answers by pressing the buttons and not using the built in keyboard. Each button press enters a number into the inputfield. The backspace button allows them to delete figures on button touch. The figures are deleted up until the final digit. When the button is pressed the error is shown in the console. If a new number is pressed it replaces the existing number as if the field had been clear. The specific error is "
FormatException: Input string was not in the correct format
System.Int32.Parse (System.String s) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System/Int32.cs:629)" I have created a script and placed it in a Manager object within the scene. I then add an on click event to the button referencing the script. The following is the full script for this function.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class BackSpace : MonoBehaviour {
public InputField number;
string numstring;
public void backspace()
{
numstring = number.text;
if (numstring.Length > 0) {
number.text = numstring.Remove (numstring.Length - 1);
} else {
number.text = "";
}
}
}
Originally the error said cs: 16, 18 as well as cs: 18, 18. I don’t know why it is now saying cs: 629.
The 629 is the line in the source code of Mono that that’s coming from, deep inside the system parse function. (note that it is Int32.cs - that’s the file.) Obviously you can’t debug that code, but if you look a little further down on that error message, it should have a stack trace, which you can follow until you find the line of code in one of your files that originally called the function that called the function that ended up having a problem at line 629 of Int32.cs. That’s the file you need to focus on.
And it’s not BackSpace - none of that code goes anywhere near anything that’d call Int32.Parse.
Could it be that some subscriber (perhaps subscribed to onValueChanged of the InputField) attempts to parse the input? Once it’d reset to string.Empty the parse method could no longer extract a valid integer and would throw, since string.Empty does not represent a valid integral number.
Or that’s done somewhere else, otherwise there’s no reason for the stacktrace to contain that information. Also if the input field is configured to only accept numbers, it wouldn’t throw afaik.
So there’s probably some extra-parsing in your code (as suggested maybe in a callback function).
StarManta, thank you so much for that post. I was able to go back and use the if statements on a referenced script and eliminate the error. Everything is working perfect now.