x


[Closed] 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;
 }
more ▼

asked Mar 08, 2012 at 10:38 AM

shaystibelman gravatar image

shaystibelman
317 42 36 40

(comments are locked)
10|3000 characters needed characters left

The question has been closed Nov 14, 2012 at 04:03 PM by Fattie for the following reason:

Other


2 answers: sort voted first

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.

more ▼

answered Mar 08, 2012 at 11:06 AM

shaystibelman gravatar image

shaystibelman
317 42 36 40

(comments are locked)
10|3000 characters needed characters left

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.
more ▼

answered Mar 08, 2012 at 11:10 AM

Bunny83 gravatar image

Bunny83
78.3k 26 107 338

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() instead.

thanks!

Mar 08, 2012 at 11:45 AM shaystibelman
(comments are locked)
10|3000 characters needed characters left

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x349
x115
x66
x8

asked: Mar 08, 2012 at 10:38 AM

Seen: 2363 times

Last Updated: Mar 08, 2012 at 11:45 AM