- Home /
Other
invalid format exception when parsing float
Hello all,
I've been creating a tele-transportation script that gets the coordinates from an sql server based database. After downloading the data and splitting it with Regex.Split, I have to parse it into a float so the vector3 gets the numbers.
My problem is that, even though it works and during gameplay everything functions, in the console I get format exception messages.
"FormatException: Invalid format. System.Double.Parse (System.String s, NumberStyles style, IFormatProvider provider)"
function teletransportation(searchname){
//create the form with the name of the point to which we want to teletransport
var sendPoint : WWWForm = new WWWForm();
sendPoint.AddField("PointName",searchname);
//send the WWWForm via WWW
var getCoords : WWW = new WWW("../DataBase/FindPoint.php",sendPoint);
yield getCoords; //Wait for the data to return
//split downloaded data
var received_data = Regex.Split(getCoords.text/*data*/,"</next>");
//divide split data into vars
pointCoords.x = float.Parse(received_data[0]);
pointCoords.y = float.Parse(received_data[1]);
pointCoords.z = float.Parse(received_data[2]);
//traslation function
gameObject.Find("Player").transform.position.x = pointCoords.x;
gameObject.Find("Player").transform.position.y = pointCoords.y;
gameObject.Find("Player").transform.position.z = pointCoords.z;
}
Answer by shaystibelman · Mar 08, 2012 at 11:06 AM
Ok, I don't know WHY that works, but I've looked at other answers and found that parseFloat() doesn't give that error, so I just used that and I no longer receive the error.
Sorry 'bout that :)
*note: if the admins want to delete this question, feel free.
Answer by Bunny83 · Mar 08, 2012 at 11:10 AM
Well, wouldn't it be interesting how your returned data looks like? That's the real problem.
I guess it's something like:
"<next>10</next> <next>20</next> <next>30</next>"
If you split it at "``" the 4 strings would look like this:
received_data[0] = "<next>10"
received_data[1] = " <next>20"
received_data[2] = " <next>30"
received_data[3] = ""
Why do you use xml for returning 3 values? Just 3 comma-seperated-values would be enough. If you have to use xml, why do you parse the xml string manually? There are many xml parsers out there.
Some further side-notes:
I'm not sure if you need pointCoords outside of the function, but it looks like it shoule be alocal variable.
pointCoords is a vector3, so why do you assign it component by component to Transform.position?
gameObject.Find("Player").transform.position = pointCoords;
is enough.Find() is a static function of GameObject and you should use the classname instead of an instance. In other words :
gameObject.Find(
-->GameObject.Find(
GameObject.Find is a quite low function. It's ok to call it once every level change, but avoid it in Update() or other cyclic called functions.
To see what float.Parse() actually have to parse, you can put a
Debug.Log(received_data[0]);
in your code.
no, the data was actually split right, i had only the numbers, since in the php creating the array i wrote: echo xValue."";
anyway, i have already resolved it usint parseFloat() ins$$anonymous$$d.
thanks!
Follow this Question
Related Questions
Float.Parse question 1 Answer
Unity3D editor not allowing large floats as input 1 Answer
how to keep fraction number ??? 1 Answer
Convert positive float to negative? 5 Answers
Float, make percentage 1 Answer