- Home /
Trying to read out of a website and wan't to store the value. Works perfect in Unity and Desktop Build. Any Ideas?
Error Code: FormatException: Input string was not in the correct format.
I got the string from the website and converted it to a float (theres just a single value on the website). It works in Unity and in Desktop build, but just not on WebGL.
My Script:
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class DataLoader : MonoBehaviour {
private int steps;
private string stepsString;
public int whileNumber = 1;
private float kilometers;
private float stepsFloat;
// Use this for initialization
IEnumerator Start () {
while (whileNumber == 1)
{
WWW databaseSteps = new WWW("http://schrittzaehler.rapperswil-jona.ch/globalsteps.php");
yield return databaseSteps;
stepsString = databaseSteps.text;
steps = int.Parse(stepsString);
stepsFloat = steps;
kilometers = stepsFloat * 0.62f / 1000;
PlayerPrefs.SetFloat("kilometersTravelled", kilometers);
Debug.Log(kilometers);
yield return new WaitForSeconds(1);
}
}
// Update is called once per frame
void Update () {
}
}
Answer by Larry-Dietz · Dec 28, 2017 at 05:53 AM
FormatException: Input string was not in the correct format means that whatever value it read from the website, was NOT a float value, nor could it be made into one. Most likely cause would be an alpha result being sent from the web page.
Verify your URL you are using. The one in your code above returns a DNS error when I tried to hit it.
My best recommendation is to verify your URL, and before you try converting it into a float, throw in a debug log to show you what was received, so you can confirm you are actually receiving a number.
Hope this helps, -Larry
I got the number already in the Debug.Log and it's just a single php script with an echo on the website which i did on my own. The strange thing is that's perfectly working on every platform (inside unity, on pc build...) but just not in the html5 format. I tried to create a new project with just a script which gets the number and gives it as an output, but it's not working either on html5 (works again on pc and inside unity). $$anonymous$$aybe Unity WebGL doesn't support the WWW function (WWW databaseSteps = new WWW("http://schrittzaehler.rapperswil-jona.ch/globalsteps.php")
That is odd.
I know the WWW routines work in WebGL. I have an app running under WebGL that hits a PHP script much the way yours does, to get an entry from a database on my server, and it doesn't have any problems.
In your test, just grabbing the results of the call to the PHP script, and displaying what was received. What was the error you got? The initial error was a format exception when trying to convert to float. If your 2nd test didn't try this conversion, I would expect a different error.
-Larry
I tried it with without the while loop and without the int.Parse and it worked again in Unity but not on WebGL (there's no Error code anymore, but there's no text/number). I also tried to read a value out of a .txt file and it also worked on pc build and in unity but again not in webgl. $$anonymous$$aybe Unity WebGL has a general problem with reading out of files/websites. I'm trying to update my Unity / WebGL, hope it works.