Resources.UnloadAsset breaks Mesh reference

Hi, I got an array of asset references and some of them are meshes. Whenever I go through the array with a for loop and unload the assets from the array, I lose the references to the meshes (only the Meshes).

 for (int i = 0; i < unloadAssets.Count; i++)
            {
                    if(unloadAssets[i]) Resources.UnloadAsset(unloadAssets[i]); 
            }

If I try to access the a mesh through the unloadAssets array after the unloading, I get a NullReference exception.

Is this an expected behavior? What if I want to load the mesh in memory again? How can I do that if the reference to the Mesh asset becomes null once the mesh is unloaded?

Yes. Unload is like that. :slight_smile:

How about use the exact same way you got it in the first place?

If there is a reason why that would not work for you, it is not apparent from your description of the problem. Generally in the lifecycle of a game, assets are loaded and unloaded constantly, as needed.

Another approach if you really want to NOT unload the meshes is either to a) not put them in the list in the first place, or b) detect that the object you are considering unloading is a mesh, and then don’t unload it.

One subtle detail that might explain your observation is that if you have this array of asset references and Instantiate them into your scene, and THEN you are unloading the original reference, keep in mind that Instantiate actually makes copies and puts them in your scene. However, it does not copy certain things, such as (I believe) Meshes, and probably other classes of assets as well. You would have to do some research to see what is actually duplicated when instantiate takes place, and what is just a reference to the original asset.

Thanks for the reply Kurt,

So my problem is as follows: I got an endless runner prototype with multiple segments spawning ahead of the player while the old ones get deleted. The segments are grouped in levels and the segments of each level use a separate set of assets. What i’m doing is, I’m making an automated unload system (UnloadUnusedAssets doesn’t work because it’s too slow), which finds the references of the assets in each segment and when that segment is destroyed, it checks if other segments ahead are using these assets, if not, then these assets get unloaded as the segment is destroyed (because they are no longer used in the game). However, if I try to re-instantiate the prefab of a segment that has already been destroyed, the prefab spawns but the scripts inside it lose their asset references. These asset references are generated during edit time in order to save performance so I can’t just go over all children of the segment and find the references again.

What I find strange is that the asset references in the prefab are lost (not talking about Mesh Filters but other scripts that have a reference to the mesh serialized in a private field). I’m not really sure how to go about that. Maybe I can save asset IDs and try to load the assets back from these IDs…

Also wanted to add that I have a couple of Scripted Object assets that hold references to some meshes and sounds and as soon as I unload the assets, those scripted objects loose their asset references and there is seemingly nothing I can do to get those back.