- Home /
Convert.ToInt32 error
Well I have this script which works fine but the problem is that I cant write in GUI.TextField. mean that I cant edit the value to convert that new value into int. When ever I try to write in the TextField I get error. FormatException : Input string was not in the correct format.
here is My Script...
using System;
void OnGUI()
{
Convert.ToInt32(GUI.TextField(new Rect(150,80,30,20),"0", 25));
}
I use this one as well but still the error.
using System;
public string stringtoedit = "123";
void OnGUI()
{
stringtoedit = Convert.ToInt32(GUI.TextField(new Rect(150,80,300,20),stringtoedit, 25));
}
and this time the error is,, Can not implicitly convert "int" to "string"
Answer by KellyThomas · Jan 27, 2014 at 05:36 PM
I suggest reading this documentation then running the following code and checking the console to see what the contents of the string are.
void OnGUI()
{
string numberString = GUI.TextField(new Rect(150,80,30,20),"0", 25);
Debug.Log(numberString);
int numberInt = Convert.ToInt32(numberString);
}
Once you have more information if you are still unable to get it working edit your question to include the exact contents of the string and we can provide further advice.
You may also find `Int32.TryParse` useful.
Thanks for the answering. your method wont work. But I figure that out. I want to share that.. here what I did...
public float editedString;
public string stringtoedit = "123";
void OnGUI()
{
stringtoedit = GUI.TextField(new Rect(150,80,30,20),stringtoedit, 25);
editedString = Convert.ToInt32(stringtoedit);
}
ok I got One more problem with it. When ever I play the game, the string contain the numbers. right. But When as soon as I remove the number then the string became NULL and all the gui disappear.. So how do I make it working?? Thanks in advance
Well that is to be expected! Convert.ToInt32()
will throw an exception when it fails it's convertion.
When an exception occurs normal execution stops and either:
jumps to a
catch
blockhalts the program
Fortunatly unity seems to run all of our user code (`Start()`/`Update()`/`OnnGUI()`/etc) in a try
block, otherwise people would crash the unity IDE and loose their work all the time.
In your case you can either use a try/catch block to handle the exception yourself allowing you to continue executing the rest of your OnGUI()
method. Or use `Int32.TryParse()` which allows for adaptive behaviour:
public string stringtoedit = "123";
void OnGUI()
{
stringtoedit = GUI.TextField(new Rect(150,80,30,20),stringtoedit, 25);
int parsedInt;
if(Int32.TryParse(stringtoedit, out parsedInt) {
// TryParse succeeded, parsedInt holds the parsed number
}
else {
// TryParse failed, parsedInt holds the default value: 0
}
}
As always Garbage In / Garbage Out if you pass a null string to a parsing fuction it will be unable to extract any meaningfull data.
Thank uuuuuuuuuuuuuuuuuuuuuuuu very very much. I want to bother you one more time, For the string I use this code to restrict my input to only numbers, I have this problem of decimals, when I write decimals in the textfield it shows this error, Input string was not in the correct format.
here is my code,
public string stringtoedit = "123";
void OnGUI()
{
stringtoedit = GUI.TextField(new Rect(150,80,30,20),stringtoedit, 25);
stringtoedit = Regex.Replace(stringtoedit, "[^0-9 .]", "");
int parsedInt;
if(Int32.TryParse(stringtoedit, out parsedInt) {
// TryParse succeeded, parsedInt holds the parsed number
}
else {
// TryParse failed, parsedInt holds the default value: 0
}
}
I use Regex.Replace to replace all the character with the given ones. It shows this decimal error.
By the way thank you for your help. I really appreciate that.