Search Unity

Bug Resources.UnloadAsset damaging Scriptable Object

Discussion in 'Editor & General Support' started by stumpytheangry, Nov 11, 2022.

  1. stumpytheangry

    stumpytheangry

    Joined:
    Jan 13, 2021
    Posts:
    8
    I may be mis-applying this but I have a (very) large Scriptable Object which contains level data.

    This data is only used once during scene creation and as it's so large it affects memory allocation so my sequence is to enter the scene, read/use the data in the Scriptable Object then release them memory with this:

    Resources.UnloadAsset(MyMapData);

    This worked well and I had no issues with it until I started to include a Material field in my Scriptable Object - now when the Resources.UnloadAsset is called it will not only unload the Asset it will also strip the Material field permanently and it needs to be re-added in the editor.

    This does not occur with either complex serialised classes or with primitives such as floats and strings etc.

    Advice very much appreciated as this is both frustrating and unexpected.
     
  2. Obay_naeem

    Obay_naeem

    Joined:
    Jan 22, 2020
    Posts:
    7
    for future seekers.
    I've face the same issue. and found the solution or what looks like the reason and the solution for it.
    if the SO that is inside the resource folder is referencing something outside the resource folder
    then it's gonna lose that reference upon unloading in the runtime.
    i was referencing a prefab in my SO (which is placed inside the resource folder) and when loading it using Resources.Load the So asset would still holds the reference
    but when using Resources.UnloadAsset the SO asset loses the prefab asset reference.
    so by placing the referenced prefab inside the Resources folder it worked normally.
    i don't know if this approach/fix is the optimal one, but it works