Using Resources.UnloadAsset? (Should I worry?)

I’ve been trying to unload the asset I load using Resources.LoadAsset with something similar to the following code:

LevelData thisLevel;

void Start(){
        thisLevel = (LevelData)Resources.Load (levelName, typeof(LevelData));
            // Get the proper variables and do some work here
        Resources.UnloadAsset (thisLevel);
}

The error I’m getting is the following:

Aside from the fact that this may be bad practice (in this scene, I’m loading and unloading multiple assets on different objects, so I want to unload them as soon as possible to empty memory for the mobile device), I can’t get this to work in other instances (ie at the end of a level). The best information I could find was from here:

In the link above, one person mentioned you had to destroy the object, call to unload the asset, and ALSO call UnloadUnusedAssets. However, destroying thisLevel doesn’t even work at all (throws another error about destroying an asset).

Any help here? Or should I not even worry and just use UnloadUnusedAssets at the end of every level? (assuming it unloads all assets that I used without the need to do UnloadAsset).

Thank you!

Hi,

If you are loading next scene, all the unused assets will be cleaned automatically. If you want to make sure, that everything is cleaned up, use an empty scene between levels, which will load the next level.

Also if you destroy GameObejct using Destroy(), you can just call UnloadUnusedAssets.

In other critical situations you can call System.GC.Collect() after UnloadUnusedAssets, to force memory to clean up immediately. It might give you a spike for performance, so use it wisely.

Thank you for the reply, proandrius. That’s one worry off my list now.