Search Unity

ScriptableObject asset and Addressables

Discussion in 'Addressables' started by Rafarel, Mar 29, 2019.

  1. Rafarel

    Rafarel

    Joined:
    Jul 21, 2017
    Posts:
    199
    Hello,

    I have some issues in my project using ScriptableObjects as data container that are not updated correctly so I've made a lot of experiments to understand what's happening.

    I have a Scene with a Manager GameObject referencing a ScriptableObject assets that holds just an int variable, let's call this asset Data.

    I also have a Prefab in my project that is referencing the same Data asset.
    My Prefab is addressable, so I load it with Addressables.LoadAsset and Instantiate with a classic Instantiate (not the addressable one).

    When my scene is playing I have access to the Data asset via the inspector at two places.
    - One on the Manager GameObject on the scene.
    - Another on the GameObject I just instantiate from the adressable Prefab that I loaded.

    If I change the value contained in the Data asset, the change is not reflected on the other GameObject, like it is two separate data object now.

    It works well on Fast mode, it does not work on Packed mode.

    If on the Manager GameObject I load only the Data asset and keep it in a public ref, I can change the Data value and the change is reflected on the asset that the Prefab instance is holding. So it can work but I'am obliged to load the Data asset from the addressable ...

    Is it a normal behaviour or not ?
    I really need help on this, thanks!
     
  2. unity_bill

    unity_bill

    Joined:
    Apr 11, 2017
    Posts:
    1,053
    Assuming I understand the situation correctly, the PackedMode is behaving correctly. When you build a few things happen...
    1. Assets that are referenced by scenes that are in the build, are baked into the build. If it's a prefab, it's actually flattened into the scene.
    2. Assets that are referenced by addressable assets are pulled into bundles as dependencies.

    So when you build, you end up with two copies of your scriptable object in the build. One pulled into the player build with the scene. One pulled into asset bundles with the asset. In fast mode, nothing is pulled anywhere, as everything is faked, so it seems like it works, but it's a lie.
     
    Ryuuguu, LudiKha, JoRangers and 2 others like this.
  3. dsavickij

    dsavickij

    Joined:
    May 17, 2019
    Posts:
    12
    So is that an intended behavior or is it a bug? please elaborate, or we can't use SOs in addressables because of different instantiating?
     
  4. LeleUnity

    LeleUnity

    Joined:
    Jul 30, 2016
    Posts:
    97

    So how should we manage this? and how to load a scriptable object into the scene with addressable? I know only LoadAssets<GameObject>...there is no type scriptable object....what are the best practices ?

    @Rafarel if you could help me too...
     
  5. JonBFS

    JonBFS

    Joined:
    Feb 25, 2019
    Posts:
    39
    @LeleUnity There's no reason for a developer to desire having a scriptable object to change in the project and scene in production (packed mode). Both Data changes as intended while you develop (fast mode).

    What are you wanting to do exactly and why?
     
  6. LeleUnity

    LeleUnity

    Joined:
    Jul 30, 2016
    Posts:
    97
    I need to Load a scriptable object at runtime with addreaddable.

    I have a WeaponInstance component which refers to WeaponSharedData which is a ScriptableObject.

    I would like WeaponInstance to load the right WeaponSharedData using Addressable name or label could you show me the code ?
     
    FolaOgunfemi likes this.
  7. JonBFS

    JonBFS

    Joined:
    Feb 25, 2019
    Posts:
    39
    It's not wise for an gameobject instance to reference an assetbundle because it won't exist. You should have the weapon instance component itself as an asset bundle so that it can correctly pull the weapon shared data scriptable object.
     
  8. LeleUnity

    LeleUnity

    Joined:
    Jul 30, 2016
    Posts:
    97
    -I do not use any assetBundle, I use Addressable.
    -I have a weapon istance component which would be attached to an empty object and set as addressable item in the addressable menu.
    -that component refers to a weapon shared data and I want it to be loaded at runtime with the right weapon data.

    Could you show the code how to do it?
    Thank you
     
    EZaca likes this.
  9. drallcom3

    drallcom3

    Joined:
    Feb 12, 2017
    Posts:
    165
    How do I handle this?
    I have prefabs already in the scene, and I have prefabs that I spawn later. Both are addressables.
    Currently all their assets get duplicated, because the addressable group is it's own self-contained world. They don't realize that the assets already exist.
    Seems like I have to go back to using /resources. I thought addressables were meant to replace that. There's also no documentation about dependencies anywhere.
     
    DungDajHjep likes this.
  10. ProtoTerminator

    ProtoTerminator

    Joined:
    Nov 19, 2013
    Posts:
    586
    @drallcom3 If your scenes are addressable and the assets are addressable, you won't have any duplicates. The individual assets will be linked as dependencies instead of baked into the scene bundles.
     
  11. drallcom3

    drallcom3

    Joined:
    Feb 12, 2017
    Posts:
    165
    I kind of have it figured out now.
    The assets need to be in a group and the scene need to be in (any) group too, like you said.
    You can't put the scene in the build (the classic way)! If you put it in the build, things will get duplicated.
    Addressables is basically it's own world and they don't talk to each other when inviting assets into the world.

    Unfortunately there's a bug where loading addressable scenes lose all their asset references on load... (tested with 1.10.0)
     
    EZaca and DungDajHjep like this.
  12. Parth521

    Parth521

    Joined:
    May 10, 2023
    Posts:
    1
    You can do one thing in this case! just create one singleton class having all your scriptable objet and refer to only this SO's everywhere. I think then you will be referring to only one SO in any case. Try this !
     
  13. SunSailor

    SunSailor

    Joined:
    Aug 22, 2012
    Posts:
    26
    There should really be a solution for this. I ran in exactly the same issue, but for another purpose. We use some kind of data binding, very similar to the event example with scriptable objects, provided by Unity. So we have a central storage for game state values, which are referenced by the GUI and a level, which is loaded as an addressable where some of these values are referenced by the prefabs already. Now, although the expectancy of the scriptable object is similar to a singleton, we have two independent instances of the ment to be same object floating around. Setting those data containers by hand contradicts the idea of having them plugged within the editor. I think this is a very sane scenario, where we should be able to rely on a specific default behavior.
     
    kyuskoj likes this.
  14. Kobald-Klaus

    Kobald-Klaus

    Joined:
    Jun 20, 2014
    Posts:
    127
    Same here - I just found out that things broke for all players out there - that's a major problem. Fortunately I can send values with remoteconfig and hot fix the setup.