Hello,
when i load an assetbundle async, then instantiate a contained object and lastly unload die assetbundle, because i don’t need it anymore, i sometimes get an error message saying:
My script looks like this:
private IEnumerator ShowObject(LoadRequest request) {
string url = request.url;
string path = "file://" + Application.dataPath + RESOURCE_PATH + url + ".unity3d";
WWW download = new WWW(path);
yield return download;
if (download.error == null) {
AssetBundle assetBundle = download.assetBundle;
//download.Dispose();
//download = null;
AssetBundleRequest abr = assetBundle.LoadAsync(url, typeof(GameObject));
yield return abr;
GameObject go = (GameObject) Instantiate(abr.asset);
assetBundle.Unload(false); // ERROR IN THIS LINE
}
else {
Debug.Log("Wenig Speicher: " + download.error);
}
loading.Remove(request);
}
I have absolutely no idea why the bundle hasn’t finished loading und why it just happens sometimes. I use the script to load objects in a certain distance, when a sphere collider around the player triggers an other simple collider, where the object will be placed.
Thanks for any help.
you load async so you are not allowed to operate on the object directly at all.
either use the normal load or use the callbacks offered for async / check in a coroutine if it is ready.
you can’t have the benefit of non-halting loading while expecting that the stuff is loaded 0.0001s later
thank you for that tip dreamora,
i thought, if i use the yield statement, then the loading process automatically waits till die bundle is loaded.
what do you mean with “use the callbacks offered for async / check in a coroutine if it is ready”?
can you give me a simple example please? =)
i would be really grateful if you help me further …
You are naturally right, yes, you can yield the async request you get back from it 
What I see missing there though is 1 or 2 yields to wait for whole frames so that the object is really loaded too with all the stuff up and ready.
In a normal situation a wait for seconds 0.5f should definitely be sufficient after the instantiate.
I think this could be the problem there as the unload happens in the same frame as the instantiate, yet no render and initialization on the object has happened. Thats my interpretation of the error message.
A general note: the unload there does not do much anyway. Sure you might think it unloads something but at least in 2.6.1 nothing will be unloaded just through Unload out of my experience, there are a few extra steps required to get back the RAM but more importantly to get it out of memory so your next attempt to load the same bundle won’t hit you with a “bundle already loaded” error