Unity Asset Bundle NullReferenceException

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:

using UnityEngine;
using System;
using System.Collections;

public class BundleLoader : MonoBehaviour{
    public string url;
    public int version;
    public IEnumerator LoadBundle(){
        using(WWW www = WWW.LoadFromCacheOrDownload(url, version)) {
            yield return www;

            if (www.error != null)
                throw new Exception("WWW download had an error:" + www.error);

            AssetBundle assetBundle = www.assetBundle;
            GameObject gameObject = Instantiate(assetBundle.Load("Cube")) as GameObject;
            assetBundle.Unload(false);
        }
        }
        void Start(){
            StartCoroutine(LoadBundle());
        }
}

I use this code to build the bundle:

// C# Example
// Builds an asset bundle from the selected objects in the project view.
// Once compiled go to "Menu" -> "Assets" and select one of the choices
// to build the Asset Bundle

using UnityEngine;
using UnityEditor;
public class ExportAssetBundles {
    [MenuItem("Assets/Build AssetBundle From Selection - Track dependencies")]
    static void ExportResource () {
        // Bring up save panel
        string path = EditorUtility.SaveFilePanel ("Save Resource", "", "New Resource", "unity3d");
        if (path.Length != 0) {
            // Build the resource file from the active selection.
            Object[] selection = Selection.GetFiltered(typeof(Object), SelectionMode.DeepAssets);
            BuildPipeline.BuildAssetBundle(Selection.activeObject, selection, path,
                                           BuildAssetBundleOptions.CollectDependencies | BuildAssetBundleOptions.CompleteAssets, EditorUserBuildSettings.activeBuildTarget);
            Selection.objects = selection;
        }
    }
    [MenuItem("Assets/Build AssetBundle From Selection - No dependency tracking")]
    static void ExportResourceNoTrack () {
        // Bring up save panel
        string path = EditorUtility.SaveFilePanel ("Save Resource", "", "New Resource", "unity3d");
        if (path.Length != 0) {
            // Build the resource file from the active selection.
            BuildPipeline.BuildAssetBundle(Selection.activeObject, Selection.objects, path);
        }
    }
}

What line gives the NRE? I assume it’s line 16, and the Cube isn’t found.

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:

if (assetBundle.LoadAll() == null) {
    Debug.Log("Null");
}

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

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?

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

hey,

In your fbx file folder, delete the “Material” folder and build. it will works fine.

1 Like