Search Unity

Insufficient error logging when using yield return LoadAsset<MonoBehaviour>()

Discussion in 'Addressables' started by frimarichard, Jan 22, 2019.

  1. frimarichard

    frimarichard

    Joined:
    Jul 24, 2017
    Posts:
    31
    I made the mistake yesterday of attempting to load an addressable via a component on the addressable gameobject.

    Code (CSharp):
    1. public AssetReference myAssetReference;
    2.  
    3. IEnumerator Stuff() {
    4.     yield return myAssetReference.LoadAsset<MyMonoBehaviour>();
    5.     Debug.Log("Loaded asset.");
    6. }
    In this case, the yield return never completed, and the log was never seen. Checking Status instead of IsDone (which is what I believe the asset enumerator checks internally) allowed the operation to complete.

    Code (CSharp):
    1. var op = myAssetReference.LoadAsset<MyMonoBehaviour>();
    2. while(op.Status == AsyncOperationStatus.None)
    3.     yield return null;
    4.  
    5. Debug.Log($"Status: {op.Status}"); // "Failed"
    6. Debug.Log($"Exception: {op.OperationException ?? "<null>"}"); // "<null>"
    Unfortunately, there was no internal error logging of any kind, and so I was lost for a good few hours attempting to figure out what was wrong.

    In this scenario, can we have an error message to say that we're doing things wrong, please?
    And can we also ensure that yield return LoadAsset() never gets stuck?

    Thanks,
    Rich
     
    phobos2077 likes this.
  2. AlkisFortuneFish

    AlkisFortuneFish

    Joined:
    Apr 26, 2013
    Posts:
    973
    Since Unity 5.0, it is no longer possible to load a component on a GameObject directly from an asset bundle, you have to get or instantiate the GameObject then GetComponent on it. Since Addressables wrap asset bundles, the same limitation applies. You can load ScriptableObject assets directly of course, but not components.
     
  3. frimarichard

    frimarichard

    Joined:
    Jul 24, 2017
    Posts:
    31
    Yep, that's fine. My problem was that the status simply returned "Failed", without any indication of why.
     
  4. AlkisFortuneFish

    AlkisFortuneFish

    Joined:
    Apr 26, 2013
    Posts:
    973
    Yeah, totally agreed with the rest of the post, was just pointing out how to fix it. Had the same head scratching situation when I encountered this.