Search Unity

  1. Unity 2019.1 is now released.
    Dismiss Notice

asset in AssetBundle != asset populated in Inspector

Discussion in 'Scripting' started by Caliber, Aug 12, 2018.

  1. Caliber


    Feb 11, 2018

    I use ScriptableObjects (SO) asset files to store item data.
    Enemies carry a list of possible items they can drop, and the list is populated in the Inspector with the item SO asset files.

    When I kill the enemy, they drop an item, and I can pick it up and add it to my inventory. If I already had an identical item in the inventory and run
    if(pickedUpItem == itemInInventory)

    it returns true, as it should. At this point everything works fine.

    Then when I save, I serialize the game data, including references to the SOs in my inventory. To load, I deserialize the data and load SOs from an AssetBundle. So upon loading, my inventory appears to be the same as when I saved, except that all my items in the inventory are populated from an AssetBundle, rather than through the inspector.

    If I at this point kill another mob, pick up an item (referenced in the Inspector) identical to one I already had in the Inventory (from an AssetBundle) and run
    if(pickedUpItem == itemInInventory)

    it returns false.

    Is this behavior expected, or should Unity automatically treat two asset files as the same one, if one is linked in the Inspector and one is loaded from an AssetBundle (meaning I messed something up)?
    If it is expected, what are my options? Do I have to stop linking assets in the Inspector if I need to compare them to assets in an AssetBundle?
  2. GroZZleR


    Feb 1, 2015
    Create a system of unique IDs for each item and compare that, so that one instance of the SO matches another instance of the SO when they represent the same thing.

    You could theoretically override the equality operator and compare every single field as well.