Unloading a UMaterial, and understanding StaticLoadObject(...)

I’m loading a USkeletalMesh in runtime and then loading a UMaterial to assign to it. I then want to swap that material for another one (changing it’s appearance). Obviously I can just load a new UMaterial but what would happen to the old one? Ideally I want to unload it so it gets collected by garbage collection but I don’t know how. Here’s how I’m loading them:



// Skeletal mesh
USkeletalMesh *skeletalMesh = Cast<USkeletalMesh>( StaticLoadObject( USkeletalMesh::StaticClass(), NULL, *pathIn ) );




// Material
UMaterial *material = Cast<UMaterial>( StaticLoadObject( UMaterial::StaticClass(), NULL, *pathIn ) );


And one more question; If I call StaticLoadObject(…) with the same path, will it create two instances of that object or will it only create one the first time it’s called, and then return a ptr to that first instance?

Thank you

The UMaterial will get garbage collected if it no longer has any references pointing to it, so you don’t have to do anything, just assign the new material. And StaticLoadObject should find the existing object first before attempting to load it. So if its already loaded, it won’t get loaded again.