Search Unity

Valid AssetBundle Throws Null Reference Exception (NRE) With Any API Call

Discussion in 'Asset Bundles' started by Aggressor, Sep 6, 2019.

  1. Aggressor

    Aggressor

    Joined:
    Aug 10, 2012
    Posts:
    62
    Hello

    I have been converting my project from using the Resource folder to AssetBundles. I have tagged my scriptable objects in the "scriptableobjects" asset bundle, and I've loaded and stored that bundle in the "Assets/AssetBundles" folder.

    Here the code where I build my assets:
    Code (CSharp):
    1.  
    2.     [MenuItem("Assets/Build AssetBundles")]
    3.     static void BuildAllAssetBundles()
    4.     {
    5.         string assetBundleDirectory = "Assets/AssetBundles";
    6.         if (!Directory.Exists(assetBundleDirectory))
    7.         {
    8.             Directory.CreateDirectory(assetBundleDirectory);
    9.         }
    10.  
    11.         //https://docs.unity3d.com/Manual/AssetBundles-Building.html
    12.         BuildPipeline.BuildAssetBundles(assetBundleDirectory,
    13.                                         BuildAssetBundleOptions.UncompressedAssetBundle,
    14.                                         BuildTarget.StandaloneWindows);
    15.     }
    After building the assets, I then go to actually load these assets and their resources:
    Code (CSharp):
    1.  
    2. using System;
    3. using System.Collections;
    4. using System.Collections.Generic;
    5. using UnityEngine;
    6.  
    7. public class X_AssetBundleManager : MonoBehaviour
    8. {
    9.     private static Maps _maps;
    10.     private static AssetBundle _scriptableObjectsBundle;
    11.  
    12.     public static T LoadScriptableObject<T>(string assetName) where T : ScriptableObject
    13.     {
    14.         if (_scriptableObjectsBundle == null)
    15.         {
    16.             AssetBundle _scriptableObjectsBundle = AssetBundle.LoadFromFile("Assets/AssetBundles/scriptableobjects");
    17.             if (_scriptableObjectsBundle == null)
    18.             {
    19.                 Debug.Log("Failed to load 'scriptableobjects' AssetBundle!"); // Does not get called!,
    20.                 return null;
    21.             }
    22.         }
    23.  
    24.         if (_scriptableObjectsBundle.Contains(assetName)) // NRE HERE
    25.         {
    26.             Debug.Log("DOES CONTAIN");
    27.         }
    28.         else
    29.         {
    30.             Debug.Log("DOES NOT CONTAIN");
    31.         }
    32.  
    33.         try
    34.         {
    35.             var all = _scriptableObjectsBundle.LoadAllAssets<ScriptableObject>(); //NRE HERE
    36.         }
    37.         catch (Exception e)
    38.         {
    39.             Debug.LogError(e);
    40.         }
    41.  
    42.         T obj = _scriptableObjectsBundle.LoadAsset<T>(assetName); //NRE HERE
    43.         if (obj == null)
    44.         {
    45.             Debug.LogError("Failed to load " + assetName + "  in scriptableobjects");
    46.         }
    47.         return obj;
    48.     }
    49. }

    The _scriptableObjects variable is not null. Its a valid object. However, calling ANY api on it causes a null reference exception.

    I can't even step through the code to see whats null. The AssetBundle itself is not null, but any call causes this.

    Any advice on how to further debug this issue or what the problem might be?
     
  2. Aggressor

    Aggressor

    Joined:
    Aug 10, 2012
    Posts:
    62
    I was overriding my static, problem solved!

    _scriptableObjectsBundle = AssetBundle.LoadFromFile("Assets/AssetBundles/scriptableobjects");
    instead of

    AssetBundle _scriptableObjectsBundle = AssetBundle.LoadFromFile("Assets/AssetBundles/scriptableobjects");