Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Addressables + Scriptable Objects help

Discussion in 'Addressables' started by AcuityCraig, Apr 6, 2020.

  1. AcuityCraig

    AcuityCraig

    Joined:
    Jun 24, 2019
    Posts:
    66
    OK I'm trying to wrap my head around this and could really use some help.
    I have scriptable objects that hold materials for different items.
    I have prefabs that are addressables that reference these scriptable objects.
    When the app starts my menu generates a button for each one of the scriptable objects so the user can click it and change the material of the item. which buttons are available are dependent on what item is selected at the time. This is done by looping through the list scriptable objects and using the trygetvalue function on the dictionary to see if the scriptable reference on the prefab exists in the dictionary - which does not work - because the reference on the addressable is pointing to a dependency and not the scriptable object bundled with the app. I tried making the SOs addressable too but this did not change anything. I even changed the list of SOs to a List of AssetReferences and generated the dictionary that way but it still does not work.

    Anyone have a recommendation on how to have the prefabs(addressables) reference scriptable objects and be able to compare them?
     
    AliAlbarrak likes this.
  2. FlightOfOne

    FlightOfOne

    Joined:
    Aug 1, 2014
    Posts:
    668
    Hi did you ever figure this out?
     
  3. AcuityCraig

    AcuityCraig

    Joined:
    Jun 24, 2019
    Posts:
    66
    Yes! Addressables create an instance, even of Scriptable Objects are instanced. So anything in my app (prefabs and scene objects) that reference those SOs broke at runtime as they were not referencing the instanced version. Instead I now use an AssetReference list to pull them in, have a global object that tracks them. Then on the prefab I have a string list of colors and when the color is requested I have a method that asks to loop through the global list and find the matching named instance SO of that color, when it finds it, it returns the necessary data.

    Its not a perfect solution, a bit hacky. But it still works without any issues.
     
    suiboli, Favo-Yang and FlightOfOne like this.
  4. FlightOfOne

    FlightOfOne

    Joined:
    Aug 1, 2014
    Posts:
    668
    @AcuityCraig Thanks for sharing!
    I ended up creating manager for SOs and ended up doing something like this:
    Code (CSharp):
    1.    
    2.   private void Init()
    3.         {
    4.             if (valueAssetRef.isDirectReference)
    5.             {
    6.                 if(ScriptableAssetsManager.TryGetScriptableAsset(valueAssetRef.UId, out ScriptableAsset scriptableAsset))
    7.                 {
    8.                     if(scriptableAsset is TAsset asset)
    9.                     {                      
    10.                         valueAssetRef = asset;
    11.                     }
    12.                 }
    13.             }
    14.         }
    Only question I have now is if I am referencing a scriptable object and if it gets instantiated, do I need to release that? or does the memory get cleared simply by setting the field (that points to instantiated SO) to null.