- Home /
The question is answered, right answer was accepted
How to change a string into integer
I have to change s string to integer
var floorname = coll.collider.gameObject.name;
var cur = floorname.Substring(5);
var cu = parseInt(cur);
But while giving print(cu); it shows value. but with a error FormatException: Input string was not in the correct format System.Int32.Parse (System.String s)
How can i clear this?
Thanks in Advance
Answer by kromenak · Dec 21, 2012 at 02:47 AM
This worked correctly for me when testing the following code:
function Start()
{
var numberString = "12";
var number = parseInt(numberString);
print(number);
}
However, if you change the "numberString" variable to some value that is not a number (like "foobar"), you get the exception you describe in your question.
It seems most likely that your "floorname.Substring" call is either not returning a string that represents a number, or it is returning a number with some extra characters, like "d12" or something. If you pass a string to parseInt that isn't actually a number, it seems to give this exception.
@Clark $$anonymous$$romenaker :But sometimes it shows error.
$$anonymous$$aybe floorname isn't what you expect in some cases. Add a debug line to show what caused the error:
var cur = floorname.Substring(5);
print("cur = "+ cur);
If you're getting floorname from some collision/trigger event, the error may occur when the object collides with another thing that doesn't have a "well behaved" name.
Answer by sona.viswam · Dec 22, 2012 at 03:35 AM
Removed whitespaces by using
cur = cur.Trim();
which works properly.
Follow this Question
Related Questions
Mid point of a object 1 Answer
Mathf.pingpong 4 Answers
getting high score of game from asp.net page 2 Answers
,Roll a ball - project 0 Answers