Search code examples
c#unity-game-engineassetbundle

AssetBundle.LoadAsset is always returning NULL


I am unable to load asset as Gameobject from Assest bundle . It is returning null

    yield return www;
    AssetBundle bundle = www.assetBundle;
    if (www.error == null)
    {
        GameObject tv = (GameObject)bundle.LoadAsset("tv");
        //yield return tv;
        //GameObject santaasset = Instantiate(bundle.LoadAsset("tv", typeof(GameObject)) as GameObject);
        Debug.Log(bundle); // returns tv           
        Debug.Log(tv);//return null
        Instantiate(tv); 
    }
    else
    {
        Debug.Log(www.error);
    }

enter image description here

updated : Before Before generating After After Generating


Solution

  • The AssetBundle.LoadAsset is returning null because you don't have an Object named "tv" in the AssetBundle you are loading.

    1.Make sure that the spelling it correct or that you are passing the correct object to the LoadAsset function. This is case-sensitive.

    2.You must make sure that your object "tv" is added to the AssetBundle before building the AssetBundle.

    Let's say that the name of your AssetBundle is "house" and the object you want to add to it is "tv", select the "tv" object and change the AssetBundle option to "house". See the the image below for reference:

    enter image description here


    In your case, the problem is #1. The name of your AssetBundle is "tv" and you want to load an Object called "1.obj". Pass "1" to the LoadAsset function not "tv".