Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Bug UnloadAsset Doesn't Work but UnloadUnusedAssets Does...

Discussion in 'Scripting' started by Dreamteck, Feb 1, 2023.

  1. Dreamteck

    Dreamteck

    Joined:
    Feb 12, 2015
    Posts:
    336
    I've been struggling with this for the past couple of days. We have a linear game which loads in levels asynchronously and unloads previous levels. When a scene is loaded, it spawns a couple of prefabs and after a while, it destroys it. When no instances are left in the scene, the scene gets unloaded.

    Of course, this doesn't unload all the assets related to the scene prefabs like meshes, textures, etc.
    Calling Resources.UnloadUnusedAssets clears the loaded assets from the memory but it's very slow. That's why we created a system that caches all unique assets for all prefabs at edit time and then upon unloading, simply loops the assets and calls Resource.UnloadAsset to remove the assets from memory.

    The only problem is... this doesn't work. The assets remain in the memory. All of them.
    In the Memory Profiler I see that the prefabs used to instantiate the objects are not unloaded and they aren't referenced by any outside managed or native objects or static fields:
    upload_2023-2-1_15-42-22.png

    Does anyone know why is nothing being unloaded?
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,797
    Did you null out the references in your caching thingy? Hanging onto them will prevent them from being fully collected, but the Destroy() should still occur on the native side.
     
  3. Dreamteck

    Dreamteck

    Joined:
    Feb 12, 2015
    Posts:
    336
    Yeah, absolutely, every time something gets destroyed, I set all of its references to null C++ style. That's why I'm struggling to understand what is happening here. Usually when you call UnloadAsset, the asset should be forcefully unloaded and every other instance of it anywhere else should disappear and become null. Not this time...