Search Unity

  1. Improved Prefab workflow (includes Nested Prefabs!), 2D isometric Tilemap and more! Get the 2018.3 Beta now.
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. Let us know a bit about your interests, and if you'd like to become more directly involved. Take our survey!
    Dismiss Notice
  4. Don't miss out on the Unite LA Keynote for the latest information from Unity! Set a reminder!
    Dismiss Notice
  5. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice
  6. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice

asset in AssetBundle != asset populated in Inspector

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

  1. Caliber

    Caliber

    Joined:
    Feb 11, 2018
    Posts:
    29
    Hi

    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

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    1,720
    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.