Search Unity

Load All Assets With Label

Discussion in 'Addressables' started by Michael_Waltham, Feb 10, 2020.

  1. Michael_Waltham

    Michael_Waltham

    Joined:
    Aug 20, 2014
    Posts:
    31
    [EDIT]:

    I have managed to load a bunch of assets successfully by Label as follows:

    Code (CSharp):
    1.         [SerializeField]
    2.         private AssetLabelReference labelToLoad;
    3.  
    4.         private void Start()
    5.         {
    6.             Addressables.InitializeAsync().Completed += OnAddressablesInitialized;
    7.         }
    8.  
    9.         private void OnAddressablesInitialized(AsyncOperationHandle<IResourceLocator> obj)
    10.         {
    11.             Debug.Log("Initialized Addressables Successfully");
    12.             Addressables.LoadAssetAsync<UnityEngine.Object>(labelToLoad.labelString);
    13.         }
    After these assets have been loaded, I then want to instantiate one of the loaded GameObjects via a AssetReference as follows:


    Code (CSharp):
    1.  [SerializeField]
    2. private AssetReference reference;
    3.  
    4. private void LoadObject()
    5. {
    6.   GameObject.Instantiate(reference.Asset);
    7. }
    I am getting a nullreference on reference.Asset even though this Asset was a part of the assets loaded previously. Any reason why?
     

    Attached Files:

    Last edited: Feb 10, 2020
  2. ProtoTerminator

    ProtoTerminator

    Joined:
    Nov 19, 2013
    Posts:
    586
    I'm guessing because AssetReference is a separate object that won't have an internal reference to the asset until you call LoadAsset. So if you call LoadAsset, the Completed should be called in the same frame or the next frame (depending how they implemented that callback internally).

    Addressables does not have a synchronous API, and they don't have any plans to add one afaik.
     
    Michael_Waltham likes this.