Hi,
I have a simple web service running on IIS. The web service has a global variable that is constantly changing by virtue of the web service’s SetVariable function being called 10 times a second by a running Flash executable.
While the Flash application is running, I have a Unity application (Windows 7, standalone) that calls the web service repeatedly to get the latest value of the global variable, and updates the x position of a game object according to the value of the global variable.
The web service works fine. Flash continually sets the variable (about 10 times a second), and Unity continually gets its latest value. However, after a few seconds, Unity freezes and no longer reports that it has successfully ‘got’ the variable value. i.e. the WWW.isDone flag is no longer being set to true.
Then, after a minute or so, the Unity app suddenly smoothly animates the test object using all the values that have been set on the server since the Unity app froze. It’s as if Unity is suddenly catching up with everything it should have been doing in the intervening time. However, this does not make sense, since I am using the same global Unity variable (mWWW) each time I call the web service - which (I assume) overwrites any previously ‘got’ data (i.e. mWWW.text is overwritten each time).
Can anyone tell me what is going on, and how to fix this so that Unity smoothly and continuously calls the web service and updates the display?
My Unity code is as follows:
========================================
var mScript_TestObject:Script_TestObject;
var mForm:WWWForm;
var mWWW:WWW;
var mWebServiceURL:String;
var mWaitingForData:boolean;
function Awake()
{
…mScript_TestObject = gameObject.FindWithTag(“TestObject”).GetComponent(Script_TestObject);
…mWebServiceURL = “http://localhost/Service1.asmx/GetValue”;
…mForm = new WWWForm();
…mForm.AddField(“tVarName”, “X”);
…getDataFromWebService();
}
function Update ()
{
…if (mWaitingForData)
…{
…if (mWWW.error != null)
…{
…print(mWWW.error);
…getDataFromWebService();
…}
…else
…{
…if (mWWW.isDone)
…{
…mWaitingForData = false;
…updateDisplay();
…getDataFromWebService();
…}
…else
…{
…print(“Waiting for data”);
…}
…}
…}
}
private function getDataFromWebService():void
{
…mWWW = new WWW(mWebServiceURL, mForm);
…mWaitingForData = true;
}
private function updateDisplay():void
{
…<SNIP: some code which gets the string, tData, from mWWW.text>
…mScript_TestObject.setPosition(tData);
}
========================================
In the TestObject script, I have the following function, which simply sets the x position of the test object (a simple cube) according to the data received from the web service:
function setPosition(tData:String):void
{
…var tX:float = -0.8 + (parseFloat(tData) * 7.6);
…transform.position.x = tX;
}
There are no other objects or scripts in the project.
========================================