Search Unity

  1. Improved Prefab workflow (includes Nested Prefabs!), 2D isometric Tilemap and more! Get the 2018.3 Beta now.
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. Let us know a bit about your interests, and if you'd like to become more directly involved. Take our survey!
    Dismiss Notice
  4. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice
  5. 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

    YongPing92

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

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

    Regards.
     
  2. Hosnkobf

    Hosnkobf

    Joined:
    Aug 23, 2016
    Posts:
    394
    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
    as
    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

    TJHeuvel-net

    Joined:
    Jul 31, 2012
    Posts:
    340
    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

    FaiyazOfficial

    Joined:
    Mar 29, 2018
    Posts:
    2
    I am having same error
    Code (CSharp):
    1.  UnityWebRequest www = UnityWebRequest.GetAssetBundle("http://localhost/TestAR/public/assets/models/tv");
    2.         yield return www.SendWebRequest();
    3.  
    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.
    err.png
     
  5. Hosnkobf

    Hosnkobf

    Joined:
    Aug 23, 2016
    Posts:
    394
    please replace your code with the following code to see all asset names in the bundle.

    Code (CSharp):
    1.      UnityWebRequest www = UnityWebRequest.GetAssetBundle("http://localhost/TestAR/public/assets/models/tv");
    2.             yield return www.SendWebRequest();
    3.    
    4.             if (www.isNetworkError || www.isHttpError)
    5.             {
    6.                 Debug.Log(www.error);
    7.             }
    8.             else
    9.             {
    10.                 AssetBundle bundle = DownloadHandlerAssetBundle.GetContent(www);
    11.  
    12.                 StringBuilder sb = new StringBuilder();
    13.                 sb.WriteLine("The bundle contains the following Assets:");
    14.                 foreach(string asset in bundle.GetAllAssetNames())
    15.                 {
    16.                     sb.WriteLine(asset);
    17.                 }
    18.  
    19.                 Debug.Log(sb.ToString());
    20.             }
    21.