Comparing Object Instance at Runtime (AssetReference)

How do I compare a normal game object with one instantiated from an AssetReference to see if they came from the same prefab?

For example:

I have a tilemap prefab that I marked as addressable. I also have a tile called TileA that I placed on the tilemap.

At runtime,

I want to iterate through the Tiles and check if any of them == TileA.
If I don’t use addressable, this is simple as the tiles placed on the tilemap can be compared to TileA.

But with addresables, then they are referencing different instances. I assume this is because the tilemap is marked addressable, it will bundle in version of TileA. Therefore the bundled version of TileA is different instance of the original tile.

I would compare it to that addressable TileA if possible but not sure how, as I don’t have asset reference to that and it’s created indirectly by Unity.

This question has been asked a lot around here and unfortunately the answer is that there’s no built-in way to do it. You have to basically just create your own identifier on the prefab (perhaps a script with a “TilePrefabId” field) to do comparisons with.

That being said - I’ve never seen this question in the context of tilemaps (nor have I ever used Tilemaps) so I’m not sure what xtra complications that introduces.

1 Like

Thanks, that’s unfortunate.

As a “work around” we have been using a custom string field or the prefab name when possible to compare. But looks like that is the “way to do it” for now. Thanks again!

An old question, but this is what works for me

var resourceLocation = Addressables.LoadResourceLocationsAsync(<YOUR_ASSET_REFERENCE>).WaitForCompletion().First();
    Debug.Log($"{resourceLocation?.PrimaryKey} : {resourceLocation?.InternalId}");

ResourceLocation.PrimaryKey is a string containing the Addressable asset ID
ResourceLocation.InternalId is a string containing the relative path to the addressable prefab asset - i.e. “Assets/MyPrefab.prefab”

1 Like