- Home /
FormatException: Input string was not in the correct format - headache
Hi folks,
I'm getting an error when I run my code, its not game breaking but its annoying and I can't figure out why its happening. I'm getting a "FormatException: Input string was not in the correct format" for the last line of the following code. I get what the error means, but the string its trying to convert to int IS a number. Anyone got any ideas?
private void LootWindow(int id) {
if(GUI.Button (new Rect(lootWindowRect.width - 30,0, closeButtonWidth,closeButtonHeight), "x")){
ClearWindow();
}
lootWindowSlider = GUI.BeginScrollView(new Rect(5, 15, lootWindowRect.width - 10, 70),lootWindowSlider,new Rect(0,0,lootItems.Count * buttonWidth, 5 * offset));
for(int cnt = 0; cnt < lootItems.Count; cnt++){
if(GUI.Button(new Rect(buttonWidth * cnt, 5, buttonWidth, buttonHeight), new GUIContent (lootItems[cnt].texture, lootItems[cnt].ID.ToString()))){
playerObject.GetComponent<Player>().inventory.Add(lootItems[cnt]);
lootItems.Remove(lootItems[cnt]);
}
theTooltip = GUI.tooltip;
Debug.Log (theTooltip);
}
DisplayFloatingTooltip();
GUI.EndScrollView();
}
private void DisplayFloatingTooltip() {
GameObject itemManager = GameObject.FindGameObjectWithTag("ItemManager");
curItem = int.Parse(theTooltip);
To clarify, the ID in "lootItems[cnt].ID.ToString()" range from 0 to 6 at the moment.
Any help would be great.
theTooltip is equal to lootItems[cnt].ID.ToString(), which is a number turned into a string. So it's a string consisting of a number.
But is it declared as a String ?
String theTooltip;
Do you have this ?
Answer by Bunny83 · May 21, 2013 at 02:31 PM
Use this Debug.Log inside DisplayFloatingTooltip right before your int.Parse:
Debug.Log (" >" + theTooltip + "< Length: " + theTooltip.Length);
and you should be able to figure out what's wrong. Maybe there's an additional space character in there.
oh, btw: Why do you assign your "theTooltip" variable inside the array? GUI.tooltip shouldn't change during the execution of OnGUI
Aha! When i'm not mousing over a button it gives me an output of > < length: 0. So i'm guessing that's when its causing a problem.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
xml : FormatException: Input string was not in the correct format 1 Answer
Distribute terrain in zones 3 Answers
How do I make this XML file parser work in Unity 1 Answer
Parse.com and Unity integration 1 Answer