Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Asset reference not the same if inside built addressable

Discussion in 'Addressables' started by Rotary-Heart, Aug 4, 2021.

  1. Rotary-Heart

    Rotary-Heart

    Joined:
    Dec 18, 2012
    Posts:
    804
    I have an addressable prefab that has a reference for a ScriptableObject. I have a dictionary that I populate as this items get instantiated, the dictionary key is for the same ScriptableObject type.

    When instantiating them I use a reference for the ScriptableObject that is not the one from the prefab, this is the one added to the dictionary. If I later try to use the one from the prefab, the dictionary doesn't find the key. At least not on a built game, didn't test in a different play mode inside the editor.

    If I print all the ScriptableObject data from both the, prefab reference and direct reference, they are both the same, so I'm guessing that when the addressables are built (since the prefab has a direct reference) it gets included in it as a different asset. Is this correct? If so, is there anyway I could have this reference without it being included in the built group?

    Here's a quick example to replicate the issue:
    Code (CSharp):
    1. public class TestSO : ScriptableObject
    2. {
    3.     public string itemName;
    4.     public string description;
    5.     public AssetReference itemPrefab;
    6. }
    7.  
    8. public class Item : MonoBehaviour
    9. {
    10.     [SerializeField]
    11.     TestSO myData;
    12.  
    13.     public TestSO MyData => myData;
    14. }
    15.  
    16. public class Spawner : MonoBehaviour
    17. {
    18.     [SerializeField]
    19.     TestSO spawnItem;
    20.  
    21.     Dictionary<TestSO, int> spawnCount = new Dictionary<TestSO, int>();
    22.     Item spawnedItem;
    23.    
    24.     async void Start()
    25.     {
    26.         Task<GameObject> loadingTask = Addressables.LoadAssetAsync<GameObject>(spawnItem.itemPrefab).Task;
    27.         await loadingTask;
    28.        
    29.         GameObject loadedItem = loadingTask.Result;
    30.         spawnedItem = Instantiate(loadedItem).GetComponent<Item>();
    31.         spawnCount.Add(spawnItem, 1);
    32.     }
    33.  
    34.     void Update()
    35.     {
    36.         if (Input.GetKeyDown(KeyCode.Space))
    37.         {
    38.             Debug.Log(spawnCount.TryGetValue(spawnItem, out _));
    39.             //This returns true
    40.  
    41.             Debug.Log(spawnCount.TryGetValue(spawnedItem.MyData, out _));
    42.             //This returns false
    43.         }
    44.     }
    45. }
     
  2. Rotary-Heart

    Rotary-Heart

    Joined:
    Dec 18, 2012
    Posts:
    804
    Bump, still trying to figure out how to make this reference work.
     
  3. LilMako17

    LilMako17

    Joined:
    Apr 10, 2019
    Posts:
    43
    i ran into this behavior as well. As a work around, I used AssetReference.AssetGUID which is the GUID string of the asset assigned to the asset reference that is accessible regardless of if the asset is loaded.