Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

[0.7.4] AssetReference.cs error and solution

Discussion in 'Addressables' started by Rotary-Heart, Apr 27, 2019.

  1. Rotary-Heart

    Rotary-Heart

    Joined:
    Dec 18, 2012
    Posts:
    813
    @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:

    Code (CSharp):
    1. [SerializeField]
    2. AssetReferenceGameObject reference;
    3.  
    4. public void LoadAsset()
    5. {
    6.     if (reference.Asset == null)
    7.     {
    8.         reference.LoadAsset();
    9.     }
    10. }
    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:
    Code (CSharp):
    1. Exception: Attempting to use an invalid operation handle
    2. AsyncOperationHandle.get_InternalOp () (at Library/PackageCache/com.unity.addressables@0.7.4-preview/Runtime/ResourceManager/AsyncOperations/AsyncOperationHandle.cs:208)
    3. AsyncOperationHandle.get_Result () (at Library/PackageCache/com.unity.addressables@0.7.4-preview/Runtime/ResourceManager/AsyncOperations/AsyncOperationHandle.cs:273)
    4. UnityEngine.AddressableAssets.AssetReference.get_Asset () (at Library/PackageCache/com.unity.addressables@0.7.4-preview/Runtime/AssetReference.cs:157)
    5. ...
    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:
    Code (CSharp):
    1. public Object Asset
    2. {
    3.     get
    4.     {
    5.         if (!m_operation.IsValid())
    6.             return null;
    7.  
    8.         return m_operation.Result as Object;
    9.     }
    10. }
    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.
     
  2. Rotary-Heart

    Rotary-Heart

    Joined:
    Dec 18, 2012
    Posts:
    813
    Issue still exists on version 0.7.5
     
  3. zvinless

    zvinless

    Joined:
    Oct 6, 2014
    Posts:
    18
    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.
     
  4. zvinless

    zvinless

    Joined:
    Oct 6, 2014
    Posts:
    18
    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?
     
  5. Rotary-Heart

    Rotary-Heart

    Joined:
    Dec 18, 2012
    Posts:
    813
    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.
     
  6. chanon81

    chanon81

    Joined:
    Oct 6, 2015
    Posts:
    168
    I agree that the suggested behavior is how it should work.
     
  7. zvinless

    zvinless

    Joined:
    Oct 6, 2014
    Posts:
    18
    @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.
     
  8. Rotary-Heart

    Rotary-Heart

    Joined:
    Dec 18, 2012
    Posts:
    813
    I don't know of any way unfortunately.
     
  9. unity_bill

    unity_bill

    Joined:
    Apr 11, 2017
    Posts:
    1,053
    It is not intentional, and I like your solution. we'll get it in the next release.
     
    Amane224, chanon81 and Rotary-Heart like this.