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)"
//create the form with the name of the point to which we want to teletransport
var sendPoint : WWWForm = new WWWForm();
//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);
pointCoords.y = float.Parse(received_data);
pointCoords.z = float.Parse(received_data);
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 = "<next>10"
received_data = " <next>20"
received_data = " <next>30"
received_data = ""
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.
gameObject.Find("Player").transform.position = pointCoords;
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); in your code.
no, the data was actually split right, i had only the numbers, since in the php creating the array i wrote:
anyway, i have already resolved it usint parseFloat() instead.
The best place to ask and answer questions about development with Unity.
If you are a new user, check out our FAQ for more information.
If you are a moderator, see our Moderator Guidelines page.
We are making improvements to UA, see the list of changes.
For troubleshooting common problems with Unity 5.x Editor (including Win 10).
To help users post good questions and use the site effectively we have posted a user guide. Please check it out.
Answers and Comments
5 People are following this question.
Error when checking if float is a whole number
How could I convert the y axis in a Vector 3 into a float?
How To Make Floating Combat Text In new UI System of 4.6
Unity doesn't Play