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;
}
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 “</next>” 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.
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.