Aggressor
Aggressor

Reputation: 13551

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

This question is not about what a null reference is, this question is about why a valid AssetBundle is throwing an NRE in an API I cannot step through

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:

public class AssetBundleManager
{
    [MenuItem("Assets/Build AssetBundles")]
    static void BuildAllAssetBundles()
    {
        string assetBundleDirectory = "Assets/AssetBundles";
        if (!Directory.Exists(assetBundleDirectory))
        {
            Directory.CreateDirectory(assetBundleDirectory);
        }

        //https://docs.unity3d.com/Manual/AssetBundles-Building.html
        BuildPipeline.BuildAssetBundles(assetBundleDirectory,
                                        BuildAssetBundleOptions.UncompressedAssetBundle,
                                        BuildTarget.StandaloneWindows);
    }
}

Before looking at the code below, here is proof that the AssetBundle is not null when it is going to be used:

enter image description here

After building the assets, I then go to actually load these assets and their resources:

public class X_AssetBundleManager : MonoBehaviour
{
    private static Maps _maps;
    private static AssetBundle _scriptableObjectsBundle;

    public static T LoadScriptableObject<T>(string assetName) where T : ScriptableObject
    {
        if (_scriptableObjectsBundle == null)
        {
            AssetBundle _scriptableObjectsBundle = AssetBundle.LoadFromFile("Assets/AssetBundles/scriptableobjects");
            if (_scriptableObjectsBundle == null)
            {
                Debug.Log("Failed to load 'scriptableobjects' AssetBundle!");
                return null;
            }
        }

        if (_scriptableObjectsBundle.Contains(assetName)) // NRE HERE
        {
            Debug.Log("DOES CONTAIN");
        }
        else
        {
            Debug.Log("DOES NOT CONTAIN");
        }

        try
        {
            // NRE HERE
            var all = _scriptableObjectsBundle.LoadAllAssets();
        }
        catch (Exception e)
        {
            Debug.LogError(e);
        }

        T obj = _scriptableObjectsBundle.LoadAsset<T>(assetName); // NRE HERE
        if (obj == null)
        {
            Debug.LogError("Failed to load " + assetName + "  in scriptableobjects");
        }
        return obj;
    }
}

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?

Upvotes: 0

Views: 802

Answers (1)

derHugo
derHugo

Reputation: 90580

in

AssetBundle _scriptableObjectsBundle = ...

you overrule (hide) your already existing field variable equally named _scriptableObjectsBundle ... so this outer field _scriptableObjectsBundle still remains null! and the one you actually assign only exists within the

if(_scriptableObjectsBundle == null) 

block → simply remove this additional AssetBundle

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

Upvotes: 2

Related Questions