Search Unity

  1. Full schedule for #UniteBerlin is now available! Featuring talks on our roadmap, hands-on labs and much more! Check it out!
    Dismiss Notice
  2. Unity 2018.1 has arrived! Read about it here
    Dismiss Notice
  3. Scriptable Render Pipeline improvements, Texture Mipmap Streaming, and more! Check out what we have in store for you in the 2018.2 Beta.
    Dismiss Notice
  4. ARCore is out of developer preview! Read about it here.
    Dismiss Notice
  5. Magic Leap’s Lumin SDK Technical Preview for Unity lets you get started creating content for Magic Leap One™. Find more information on our blog!
    Dismiss Notice
  6. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice

Converting Asset Bundle to GameObject returns Null GameObject

Discussion in 'Scripting' started by YongPing92, May 15, 2018.

  1. YongPing92


    Feb 12, 2018
    Code example:
    return (GameObject)_bundle.LoadAsset(_assetName);

    bundle is loaded, it is not null,
    but it returns null Game Object...
    Anybody have an idea?

  2. Hosnkobf


    Aug 23, 2016
    actually, it should throw an exception with your code... Maybe a mistake in unity's mono implementation... but to have it null if the cast fails you normally must use the
    keyword. Or even better: use the generic function instead:
    Code (csharp):
    1. // with as keyword:
    2. // returns null if the result is null or cannot be cast to GameObject
    3. return _bundle.LoadAsset(_assetName) as GameObject;
    4. // Generic version:
    5. // has the same behavior as with 'as' but is a bit better (and more secure) to read and write
    6. return _bundle.LoadAsset<GameObject>(_assetName);
    So, I have no idea why it returns null instead of throwing an exception.
    But your problem is probably that the object you are loading is not a game object or the asset name (or path to it) is not correct.
    Last edited: May 15, 2018
  3. TJHeuvel-net


    Jul 31, 2012
    You can have an idea! Why dont you log the output of your LoadAsset, see if its null (that means the name cannot be found) or maybe of another type that you arent expecting.
  4. FaiyazOfficial


    Mar 29, 2018
    I am having same error
    Code (CSharp):
    1.  UnityWebRequest www = UnityWebRequest.GetAssetBundle("http://localhost/TestAR/public/assets/models/tv");
    2.         yield return www.SendWebRequest();
    4.         if (www.isNetworkError || www.isHttpError)
    5.         {
    6.             Debug.Log(www.error);
    7.         }
    8.         else
    9.         {
    10.             AssetBundle bundle = DownloadHandlerAssetBundle.GetContent(www);
    11.             GameObject tv = (GameObject)bundle.LoadAsset("tv");
    12.             yield return tv;
    13.             Debug.Log(bundle); // logs tv
    14.             Debug.Log(tv); // logs Null
    15.             Instantiate(tv);
    16.         }
    Same error with WWW as well.