- Home /
www.data and string conversion to integer
I am new at Unity but trying to utilize www to get information from php. I have read I think almost all the forum and answer posts.
I get an error message that string is not in correct format? Can anyone help? I am send back the number 10 from php in an echo "10" - I am also wondering is there is other documentation on string parsing other than in the reference guide?
php code ->
<?php
echo "10" ?>
java code ->
This is the code:
function Start () { //var data : string; // call php page; var www : WWW = new WWW (url);
// Wait for download to complete; yield www;
// returns back string from php - not in form that is workable; speedA = www.data;
// parse command seems to work because you can hard code value for speedA; speeds = int.Parse(speedA);
}
Answer by jashan · Aug 25, 2010 at 07:19 PM
The first thing you should to do is print www.data and see what exactly it looks like. Most likely, this is not just "10" but maybe "10 plus return" or something like that.
Something like
Debug.Log(string.Format(" x{0}x ", speedA));
should do the trick. If this prints anything else than x10x, like x10 new line x, for instance, that would be the problem. You might try String.Trim() ... that should remove whitespace:
speedA = speedA.Trim();
Of course, for that to work you have to make sure that speedA really is a string but I guess UnityScript should handle that correctly for you.
I think the problem is in the www.data string not sure how to use the Trim command but will look at that.
I've edited the answer to make usage of Trim() a bit more clear. I had it in the original answer but now it should be super-easy ;-)
Answer by DaveA · Aug 25, 2010 at 08:30 PM
I thought int.Parse() was C#, but with .Net, learn something new every day. I just converted a JS to C#, and I was using parseInt() in JS (worked fine), replaced with int.Parse() for C#. So you might try speeds = parseInt(speedA);
Your answer
Follow this Question
Related Questions
WWW.text gets extra analytics code to the string!?! 3 Answers
Getting String From WWW 2 Answers
Retrieving varibale values from a php script? 1 Answer
How to convert a string to a type WWW 0 Answers