- Home /
Yield Problems
Helo, i'm trying to use WWW to get an assetbundle on a local server. The problem is when i try to use "yield" to get the asset i get the following error message :
Failed to load asset bundle
UnityEngine.WWW:get_assetBundle()
Here's the code :
//Default dimensions for the text area
public int defWidth = 500;
public int defHeight = 25;
//Object on which the material will be applied
public GameObject obj;
//Used to check if the url was changed | avoid reloading the file
private string lastUrl = "Enter the adress";
//Te actual variable containing the url of the file
private string url = "Enter the adress";
//The variable containing the adress of WWW method
private WWW wwwTest = null;
//The variable containing the adress of the material
private Material mat;
private void LoadPart(string link)
{
if (lastUrl != link)
{
StartCoroutine(GetAsset(link));
if (wwwTest.error != null)
Debug.Log(wwwTest.error);
}
}
private IEnumerator GetAsset(string link)
{
AssetBundle ab;
wwwTest = new WWW(link);
yield return wwwTest;
ab = wwwTest.assetBundle;
}
void OnGUI()
{
int lastYPos = 0;
int textAreaHeight = defHeight;
int textAreaWidth = defWidth;
int buttonWidth = 50;
int buttonHeight = 20;
url = GUI.TextField(new Rect((Screen.width - textAreaWidth) / 2, lastYPos, textAreaWidth, textAreaHeight), url);
lastYPos += textAreaHeight;
if (GUI.Button(new Rect((Screen.width - buttonWidth) / 2, lastYPos, buttonWidth, buttonHeight), "Load"))
LoadPart(url);
lastYPos += buttonHeight;
}
This is only a testing code to see how assetbundles work, but so far i failed to understand how to use it with or without coroutines.
Edit : this error is the same i had when i wrote the code without a coroutine. I don't uderstand why it fails to load, when i try to import the assetbundle from the editor it works fine. In there is only a material called "Ananas" with texture of ananas skin.
Answer by ks13 · Dec 14, 2011 at 01:47 PM
Wow, i don't want to believe this, but it seems some people don't even read the code before answering. As it turns out, the yield wasn't the problem. I checked every part i yielded and everything went fine, even the size of the file loaded was the same. The only thing was the failing of loading. After much checking, it seems i was building my bundles wrong, and that's why WWW couldn't load them.
Just saying in case someone else has the same problem, if :
- isDone = true
- error = null
- size = size of the bundle
then the problem is most likely the bundle itself.
Answer by Eric5h5 · Dec 14, 2011 at 09:53 AM
You're not yielding on the GetAsset coroutine, as the error says.
Uh, ok, can you give a little more details? Because from the examples i've seen in Unity Scripting $$anonymous$$anual or codes shown in here, it's the way to do it. Which is obviously wrong seeing the error, but i don't see how else to do it.
Edit : ok, forgot to change the name of the WWW variable to the new name, updated the post with the new error.
$$anonymous$$ight want to take another look at the docs...yield return new StartCoroutine... Otherwise it just returns immediately, hence the error.
StartCoroutine only starts the coroutine so it's normal it returns immediately, but the coroutine started continues executing, that's why i used global variables and yield inside the started coroutine. Unless i misunderstood something, i think that's the way to use it.
The way i coded it, it should start a coroutine, GetAsset, that's going to download and load the wanted asset, while downloading with WWW, i pause GetAsset with yield, then load the asset. That's the intended behaviour. Similar to this example. Am i wrong?
Yes, that's wrong, please read what I wrote again; also look at the second example on that page, not the first.
Ok, sorry but you lost me here. I've looked at the second example and don't uderstand what you're hinting at. Unless you're saying i have to use WaitForSeconds...
Edit : I forgot to mention this but, this code works when i try to load and diplay a jpg image. This is why i think the loading should work, and the code is properly structured.
Your answer
Follow this Question
Related Questions
How to set timer for WWW helper? 1 Answer
yield return WWW stops Coroutine? 0 Answers
loading asset bundle function 1 Answer
yield return request never returns 2 Answers
yield on a www never completes 10 Answers