I cannot load an asset in web player with instantiate

Hi i a very new to unity, I have a problem with instantiate in web build.
I cannot load an object in web player. When i try to load it in editor it loads perfectly.

This is the code that i use

if (Application.isEditor) {
assetPath = “file:///” + Application.streamingAssetsPath + “/” + thumb + goniakos + “" + width + length + “.unity3d”;
var eee = Application.streamingAssetsPath + “/” + thumb + goniakos + "
” + width + length + “.unity3d”;
}
else {

message += " isNotEditor";
assetPath = Application.dataPath + "/StreamingAssets" + "/" +   thumb  + goniakos + "_" + width + length +  ".unity3d";
eee = Application.dataPath + "/StreamingAssets" + "/"  + thumb  + goniakos + "_" + width + length +  ".unity3d";
		}

message += assetPath;
var www = new WWW(assetPath);
		
var instance : GameObject = Instantiate(www.assetBundle.mainAsset);

message += " isNotEditor";

thumb, width, and length are variables

When i print the variable assetPath its a valid http address but the object doesent load

Thank you

1 Answer

1

In the web player, you need to give it time to download. Between “new WWW()” and “Instantiate()”, call “yield return www” or check “isDone”:

var www = new WWW(assetPath);
yield www;
var instance : GameObject = Instantiate(www.assetBundle.mainAsset);

> I tried that but the above code is inside a OnGUI function and the yield throws an error, that says OnGUI() cannot be a coroutine Then instead of "yield www", check www.isDone. When www.isDone is true, this means the download is done and you can instantiate objects.

can i use www.isDone with javascript?

Yes. Just keep checking each OnGUI() until www.isDone returns true. Refer to: http://docs.unity3d.com/Documentation/ScriptReference/WWW.html

if((www.isDone == true)){ this doesent work what am i doing wrong?

What error does it report? Consider moving your code out of OnGUI() and using yield. For example, the sample code is in Start(). (See the WWW.html link in my previous comment for the sample code.)