The AssetBundle can't be loaded

When i start the game the AssetBundle is working fine, but when i go in a other scene and i come back i have that error:

using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class UpdateGame : MonoBehaviour {

    public string bundleUrl;
    public RawImage lastVersion;

    AssetBundle assetBundle;
    int correctAnswer;

    void Start()
    {
        LastVersionN(1);
    }

    public void LastVersionN (int questNumber)
    {
        if(!assetBundle)
        {
            StartCoroutine(LoadAssetBundle(questNumber));
            return;
        }
        StartCoroutine(ShowVersion(questNumber));
    }

    IEnumerator LoadAssetBundle (int questNumber)
    {
        WWW www = new WWW(bundleUrl);
        yield return www;
        assetBundle = www.assetBundle;
        LastVersionN(questNumber);
    }

    IEnumerator ShowVersion (int questNumber)
    {
        AssetBundleRequest assetTexture = assetBundle.LoadAssetAsync<Texture2D>("LV" + questNumber);
        yield return assetTexture;
        lastVersion.texture = assetTexture.asset as Texture2D;
    }
}

When you load an asset bundle it stays in memory so essentially you don’t need to load it again.

You can either save a reference to it, so you can check if you e loaded it already, or you can unload it after you’ve created the assets you need to load.

You may gut also consider keeping a reference to assets you already have instances. So you could clone those rather than having to load the asset bundle again.

All of this is something you have to write code for.

How i can check if the loaded bundle is already loaded?

I tried your link, but i have the same error and 2 more:
2992952--222888--Error.png

Code:

using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class UpdateGame : MonoBehaviour {

    public string bundleUrl;
    public RawImage lastVersion;

    AssetBundle assetBundle;
    int correctAnswer;

    void Start()
    {
        LastVersionN(1);
    }

    public void LastVersionN (int questNumber)
    {
        if(!assetBundle)
        {
            StartCoroutine(LoadAssetBundle(questNumber));
            return;
        }
        StartCoroutine(ShowVersion(questNumber));
    }

    IEnumerator LoadAssetBundle (int questNumber)
    {
        while (!Caching.ready)
            yield return null;

        WWW www = WWW.LoadFromCacheOrDownload(bundleUrl, 5);
        yield return www;

        if (!string.IsNullOrEmpty(www.error))
        {
            Debug.Log(www.error);
            yield return null;
        }

        assetBundle = www.assetBundle;

        var asset = assetBundle.mainAsset;

        //WWW www = new WWW(bundleUrl);
        //yield return www;
        //assetBundle = www.assetBundle;
        LastVersionN(questNumber);
    }

    IEnumerator ShowVersion (int questNumber)
    {
        AssetBundleRequest assetTexture = assetBundle.LoadAssetAsync<Texture2D>("LV" + questNumber);
        yield return assetTexture;
        lastVersion.texture = assetTexture.asset as Texture2D;
    }
}

Then you’ll have to keep a reference that survives scene loads or unload it before changing scenes.

In original code, where i need to put unload line?, i don’t know much about Assets Bundles.

I’d first try doing it in OnDestroy - so when the object gets destroyed because of the scene change it unloads the bundle.

I have resolved this problem using add below lines,

private AssetBundle myLoadedAssetBundle;
string bundleUrl=“”;
IEnumerator LoadAssetBundle ()
{
if (myLoadedAssetBundle != null) {
myLoadedAssetBundle.Unload(false); //scene is unload from here
}

while (!Caching.ready)
yield return null;

WWW www = [WWW.LoadFromCacheOrDownload(bundleUrl](http://WWW.LoadFromCacheOrDownload(bundleUrl), 1);
yield return www;

if (!string.IsNullOrEmpty(www.error))
{
Debug.Log(www.error);:wink:
yield return null;
}
myLoadedAssetBundle = www.assetBundle;
}

Hi I tried everthing like as myLoadedAssetBundle.Unload(false) after Instantiate but it does’t works.the first time Instantiate does work but then I reload scene (Run LoadFromCacheOrDownload again) received error :
“can’t be loaded because another assetbundle with the same files is already loaded.”
Please for me solutions .Thanks.

You can’t load the same asset bundle again, nor you can have two identically named asset bundles loaded at the same time. If bundle is already loaded - just use it. If you have multiple bundles with the same name, you have to unload the bundle then load a different one.
See documentation for AssetBundle class on how to get loaded bundles and unload them.

2 Likes

@Aurimas-Cernius Thanks.I used this method and it’s doest work well.
AssetBundle.UnloadAllAssetBundles(true);

@AmobearGame @Aurimas-Cernius I have tried AssetBundle.UnloadAllAssetBundles(true); but not working. Whats the proper way to unload all loaded AssetBundles.

@Aurimas-Cernius

Hi,
I am using “AssetBundle.GetAllLoadedAssetBundles()” to load already loaded AssetBundles.It loads normal AssetBundles but it doesn’t load scene bundles.Any idea??

Hi,

In my case for Scene Asset Bundles. I replaced my AssetBundle.GetAllLoadedAssetBundles() by a custom List I manage myself.
I put in the list every AssetBundle I’m loading.
and before each Load I check the list to see if the bundle is still present.
The list might contains some ‘null’ items. (I presume they are unloaded AssetBundles)
Make sure to clean up ‘null’ items in the list.

        private static Dictionary<string, AssetBundle> loadedAssetBundleMap = new Dictionary<string, AssetBundle>();

       public static AssetBundle GetAssetBundle(string assetBundleName)
       {
           foreach (var kv in loadedAssetBundleMap)
               if (kv.Key == assetBundleName)
                   return kv.Value;
           return null;
       }

        public AssetBundle AssetBundle
        {
            get
            {
                AssetBundle bundle = GetAssetBundle(assetBundleName);

                if (bundle == null)
                {
                    bundle = AssetBundle.LoadFromMemory(_bytes);
                    loadedAssetBundleMap[assetBundleName] = bundle;
                }
                return bundle;
            }
        }
1 Like

This still does not seem well handled or explained by Unity after UnityWebRequest has replaced WWW.

This error is thrown out of the blue, it isn’t explained fully or in the manual as far as I can see. The relevant function (DownloadHandlerAssetBundle.GetContent) should return a result with an error code/message, not have the runtime log an error!

Even if you do your best to avoid duplicate bundle loads it seems if the bundle contents are the same - maybe just by names?, but a different file, it can still throw the error (what is the check?). This can be a problem in some art cases.
Either way the error should be detailed and you should be able to handle it - catch it.

3 Likes

I’m in the same boat as @andyz : in my code, I need to be able to handle this error. I’ve tried to wrap it in a try-catch, but that doesn’t do anything. And there doesn’t seem to be any way to query what bundles are currently loaded.

I’m downloading a bunch of asset bundles via concurrent coroutines. After I load the asset needed from the bundle, I immediately call bundle.Unload(false);. However, if I happen to be downloading the same bundle twice, in two different coroutines, I’ll sometimes get this error message. Since coroutines are all happening on the main thread, this means that there is some amount of time that bundles remain persistent in memory after calling bundle.Unload. So there doesn’t seem to be any way to avoid this bug.

@Aurimas-Cernius , any ideas for workarounds?

Well, that is your problem.
You have to manage things in a way that you don’t load the same asset bundle twice. You can’t do random things in random places and expect consistent behavior.

Right, I’ve already been working on that part. But we should be able to load an asset bundle, unload it, and then load it again. And, really, the bigger issue is that this error is written to the console but produces no exception that we can catch and handle ourselves. As @andyz mentioned:

“This error is thrown out of the blue, it isn’t explained fully or in the manual as far as I can see. The relevant function (DownloadHandlerAssetBundle.GetContent) should return a result with an error code/message, not have the runtime log an error!”

Is that issue captured anywhere on the roadmap?

In latest Unity versions we have introduced new set of APIs for such purpose. Not exactly ideal yet, but a step.
https://docs.unity3d.com/2020.2/Documentation/ScriptReference/Networking.DownloadHandler-error.html