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:
public class TestSO : ScriptableObject
{
public string itemName;
public string description;
public AssetReference itemPrefab;
}
public class Item : MonoBehaviour
{
[SerializeField]
TestSO myData;
public TestSO MyData => myData;
}
public class Spawner : MonoBehaviour
{
[SerializeField]
TestSO spawnItem;
Dictionary<TestSO, int> spawnCount = new Dictionary<TestSO, int>();
Item spawnedItem;
async void Start()
{
Task<GameObject> loadingTask = Addressables.LoadAssetAsync<GameObject>(spawnItem.itemPrefab).Task;
await loadingTask;
GameObject loadedItem = loadingTask.Result;
spawnedItem = Instantiate(loadedItem).GetComponent<Item>();
spawnCount.Add(spawnItem, 1);
}
void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
{
Debug.Log(spawnCount.TryGetValue(spawnItem, out _));
//This returns true
Debug.Log(spawnCount.TryGetValue(spawnedItem.MyData, out _));
//This returns false
}
}
}