yield problem at return value from WWW

Hi everyone,

I’ll first say sorry about being a newbie in coding JS.

Here is my problem.

From a Mysql Database, I write a position in a Php file. In my unity routine, I’d like to get this float position in order to move an object from the downloaded position.
How do I get a correct float return from the getPosition return without having this mistake at compiling.
(16,1): BCE0038: ‘System.Collections.IEnumerator’ is not a valid macro.
position.php contains some easy database fetch and can be sum up by Php ‘echo $row[‘Position’] . " " ;’

Thank you everyone for some help.
No7.

Here is my code :

var PlayerSpeed : int = 9.0;
IEnumerator getPosition():float
{
   	    var hs_get = new WWW("http://www.mysite.com/position.php");
	    yield hs_get;
	 
	    if(hs_get.error)
	    {
	    	print("There was an error getting the position : " + hs_get.error);
	    	return 0;
	    }
	    else
	    {
	       Debug.Log(hs_get.text);
	       return parseFloat(hs_get.text);
	    }
 
}

function Update () {
		
		amountToMove = (PlayerSpeed * getPosition()* Time.deltaTime);
		transform.Translate (Vector3.right * amountToMove);

}

Thats right, coroutines do not return values. You need to assign the value you are fetching from your WWW request into a variable you access else where.

var PlayerSpeed : int = 9;
var hs_get : float = 0.0; // add this line

function Start()
{
    StartCoroutine(getPosition());
}

function getPosition()
{
        var www : WWW = new WWW("http://www.mysite.com/position.php");
        yield www;
 
        if(www.error)
        {
           print("There was an error getting the position : " + www.error);
           hs_get = 0.0;
        }
        else
        {
           Debug.Log(www.text);
           hs_get = hs_get.parseFloat(www.text);
        }
 
}
 
function Update () {
 
       amountToMove = (PlayerSpeed * hs_get * Time.deltaTime);
       transform.Translate (Vector3.right * amountToMove);
}

This is untested but I think it should work.

EDIT: You probably don’t want the code in the update function to run until you have retrieved your position from your url. Consider using a boolean and wrapping what is in the update function in it.

Hi SubatomicHero,
Here is the code I’ve implemented to get the position at each update frame… But i’ve still have a too many threads crash.
If I want to have a constant check of position I need to cancel the

 function Start()
 {
     StartCoroutine(getPosition());
 }

Here is my complete code… adaptation of yours :-).

var amountToMove : float = 0.0;
var amountToMove2 : float = 0.0;
var hasDownloaded : boolean = false;

function getPosition()
{
	    var hs_get = new WWW("http://www.mysite.com/display.php");
	    yield hs_get;
	 
	    if(hs_get.error)
	    {
	    	print("There was an error getting the download : " + hs_get.error);
	    }
	    else
	    {
	       Debug.Log(hs_get.text); 
	       amountToMove = parseFloat(hs_get.text);
	       hasDownloaded = true;
	    }
}



function Update () {

	
		if (hasDownloaded)
	   	{
			
			amountToMove2 = (PlayerSpeed * amountToMove );
			transform.Translate (Vector3.right * amountToMove2);
			hasDownloaded = false;
		}
		else getPosition();
}