Hi - I am seeing some odd behavior when I call an accessor function in a script that uses the WWW class. If I call one particular accessor function I consistently get the correct results. If I call others I get “UnityEngine.Coroutine” - instead of the property I wanted.
This makes no sense to me and I don’t see what I am doing wrong.
–Roy
–this is the code I use to instance a prefab and call the attached script. The instance is stored in a variable.
I then call the function to start the www operation: loader.StartLoad(url, urlType);
When the www operation completes it calls a callback function in a go:
function webOpComplete(id){
loader = wwwLoader_array[id];
script = loader.GetComponent("rp WWW Loader");
//print(script);
s = script.GetStatus();
print(s);
theError = script.GetWWWtype();
print(theError);
}
This the result:
private var callbackObject: GameObject;
private var callbackFunction: String;
private var wwwURL: String;
private var wwwType: String;
private var wwwID: Number;
private var www;
private var WWWinProgress = false;
private var tempTexture;
private var wwwError: String;
private var arrayIndex;
//
function SetUp(callbackObj, callback, id){
callbackObject = callbackObj;
callbackFunction = callback;
wwwID = id;
wwwType = "none";
}
function StartLoad(url: String, urlType: String){
wwwURL = url;
wwwError = "ok";
wwwType = urlType;
www = WWW(wwwURL);
WWWinProgress = true;
yield www;
//
if ([url]www.isDone[/url]){
WWWinProgress = false;
//
if ([url]www.error[/url] != null) {
wwwError = [url]www.error;[/url]
}
callbackObject.SendMessage(callbackFunction, wwwID);
}
}
function GetStatus(){
return WWWinProgress;
}
//
function GetArrayIndex(){
return arrayIndex;
}
function GetWWWerror(){
return wwwError;
}
function GetWWWtype(){
return wwwType;
}
function GetWWWURL(){
return wwwURL;
}
The function you are calling is a coroutine. Any function containing yield is a coroutine. A coroutine cant return any value, since it doesnt return immediately but can continue running after the function call. Coroutines always return UnityEngine.Coroutine, this is so you can yield the function, thus waiting until the coroutine is completed.
thanks for the reply. this is still confusing though since one of my function calls does work, and I don’t call any functions in the instance at all until the yield completes.
When I swap the return value from the string in the above example to a number (0 for example), the printed value in the calling function is no longer coroutine but (correctly) 0.
Bug?
Workaround is obviously to directly access the value (myParent.shipType), but it seems weird that returning a string doesn’t work. I was taught to access stuff via functions and not directly…seems I have to start cheating.
There seems to be some issue related to automatic propagation of coroutines and dynamic typing in your example.
To work around it until we fix it in the compiler, you can just make sure that the instance you are GetShipType on is statically type.
eg. make sure myParent is an exact type.
var myParent : MyShipScript;