Search Unity

Unity Asset Bundle NullReferenceException

Discussion in 'Editor & General Support' started by alexisrabadan, Oct 20, 2014.

  1. alexisrabadan

    alexisrabadan

    Joined:
    Aug 26, 2014
    Posts:
    82
    I have had asset bundles working before and understand how they work, but my most recent project that uses them won't work.

    I get the error when trying to play in the editor:
    NullReferenceException: Object reference not set to an instance of an object

    I can make it work with a local bundle, but not when downloading from my website

    I use this code to load the bundle:

    Code (CSharp):
    1. using UnityEngine;
    2. using System;
    3. using System.Collections;
    4.  
    5. public class BundleLoader : MonoBehaviour{
    6.     public string url;
    7.     public int version;
    8.     public IEnumerator LoadBundle(){
    9.         using(WWW www = WWW.LoadFromCacheOrDownload(url, version)) {
    10.             yield return www;
    11.  
    12.             if (www.error != null)
    13.                 throw new Exception("WWW download had an error:" + www.error);
    14.  
    15.             AssetBundle assetBundle = www.assetBundle;
    16.             GameObject gameObject = Instantiate(assetBundle.Load("Cube")) as GameObject;
    17.             assetBundle.Unload(false);
    18.         }
    19.         }
    20.         void Start(){
    21.             StartCoroutine(LoadBundle());
    22.         }
    23. }
    I use this code to build the bundle:

    Code (CSharp):
    1. // C# Example
    2. // Builds an asset bundle from the selected objects in the project view.
    3. // Once compiled go to "Menu" -> "Assets" and select one of the choices
    4. // to build the Asset Bundle
    5.  
    6. using UnityEngine;
    7. using UnityEditor;
    8. public class ExportAssetBundles {
    9.     [MenuItem("Assets/Build AssetBundle From Selection - Track dependencies")]
    10.     static void ExportResource () {
    11.         // Bring up save panel
    12.         string path = EditorUtility.SaveFilePanel ("Save Resource", "", "New Resource", "unity3d");
    13.         if (path.Length != 0) {
    14.             // Build the resource file from the active selection.
    15.             Object[] selection = Selection.GetFiltered(typeof(Object), SelectionMode.DeepAssets);
    16.             BuildPipeline.BuildAssetBundle(Selection.activeObject, selection, path,
    17.                                            BuildAssetBundleOptions.CollectDependencies | BuildAssetBundleOptions.CompleteAssets, EditorUserBuildSettings.activeBuildTarget);
    18.             Selection.objects = selection;
    19.         }
    20.     }
    21.     [MenuItem("Assets/Build AssetBundle From Selection - No dependency tracking")]
    22.     static void ExportResourceNoTrack () {
    23.         // Bring up save panel
    24.         string path = EditorUtility.SaveFilePanel ("Save Resource", "", "New Resource", "unity3d");
    25.         if (path.Length != 0) {
    26.             // Build the resource file from the active selection.
    27.             BuildPipeline.BuildAssetBundle(Selection.activeObject, Selection.objects, path);
    28.         }
    29.     }
    30. }
     
  2. Graham-Dunnett

    Graham-Dunnett

    Administrator

    Joined:
    Jun 2, 2009
    Posts:
    4,287
    What line gives the NRE? I assume it's line 16, and the Cube isn't found.
     
  3. alexisrabadan

    alexisrabadan

    Joined:
    Aug 26, 2014
    Posts:
    82
    Yes, its line 16, I am downloading a cube object as a test. I checked to see if the asset bundle downloaded contained anything using:

    Code (CSharp):
    1. if (assetBundle.LoadAll() == null) {
    2.     Debug.Log("Null");
    3. }
    This confirms that nothing is being downloaded

    This is really annoying, it is the only feature that my team needs pro for at the moment and it is not working
     
  4. Graham-Dunnett

    Graham-Dunnett

    Administrator

    Joined:
    Jun 2, 2009
    Posts:
    4,287
    Does the assetBundle have a size? If you drop 10 cubes into the asset bundle does the file get larger? How many objects are in the Selection when you use the menu to make the asset bundle? Does activeBuildTarget match the target you are making the asset bundles for?
     
  5. alexisrabadan

    alexisrabadan

    Joined:
    Aug 26, 2014
    Posts:
    82
    Thanks for your help so far Graham

    -The size of the asset bundle is 1.75kb
    -The size with 10 cubes is 2.17kb
    -I am building for Mac/PC/Linux and trying to get it working in the editor
    -I highlight the prefabs in the "project" window then either right click them and select "build asset bundle from selection - track dependencies" or "Assets" > "build asset bundle from selection - track dependencies"

    Tested with both web player and unity editor, both are not working

    Using Unity 4.6 beta 20
     
  6. metalpulp

    metalpulp

    Joined:
    Sep 26, 2015
    Posts:
    1
    hey,

    In your fbx file folder, delete the "Material" folder and build. it will works fine.
     
    LykenTech likes this.