[0.7.4] AssetReference.cs error and solution

@unity_bill There’s an error on the Asset property of the script that makes it throw an error when you try to access the property to check if something is loaded.

Using the following simple code to illustrate the problem:

[SerializeField]
AssetReferenceGameObject reference;

public void LoadAsset()
{
    if (reference.Asset == null)
    {
        reference.LoadAsset();
    }
}

As you can see nothing special is happening here, it just checks if an asset has been loaded before trying to load it. This will throw the following error:

Exception: Attempting to use an invalid operation handle
AsyncOperationHandle.get_InternalOp () (at Library/PackageCache/com.unity.addressables@0.7.4-preview/Runtime/ResourceManager/AsyncOperations/AsyncOperationHandle.cs:208)
AsyncOperationHandle.get_Result () (at Library/PackageCache/com.unity.addressables@0.7.4-preview/Runtime/ResourceManager/AsyncOperations/AsyncOperationHandle.cs:273)
UnityEngine.AddressableAssets.AssetReference.get_Asset () (at Library/PackageCache/com.unity.addressables@0.7.4-preview/Runtime/AssetReference.cs:157)
...

Which makes perfect sense since the m_operation field on the script is only valid when it loads something. So if the property in the AssetReference.cs is changed to:

public Object Asset
{
    get
    {
        if (!m_operation.IsValid())
            return null;

        return m_operation.Result as Object;
    }
}

It works without any problems since it will be returning a null value. I’m not sure if I should report this with the bug report tool since it’s not an engine error.

Issue still exists on version 0.7.5

I was also doing this check prior to 0.7.4 in order to avoid double incrementing the ref counter. I don’t really know how to work around it without reflection or try/catching the null check.

Looks like this is the same in 0.8.4. Is this something that’s intentional (as in we should avoid checking this value and doing so should throw an exception if it’s invalid), or is this change something the Addressables team might consider?

Well you can just modify the script how I say above. I’m not sure if this is intended, that’s something that only @unity_bill can answer.

I agree that the suggested behavior is how it should work.

@Rotary-Heart is there a simple way to edit a package and have it propagate to my teammates? My understanding is that I’d need to either host the modified package and point to that or copy it into the project directly.

I don’t know of any way unfortunately.

It is not intentional, and I like your solution. we’ll get it in the next release.

3 Likes