SetSkeletalMesh() keeps some materials from old mesh

I want to change a character’s mesh during runtime. The mesh does change, but the some materials are old. Say I have a mesh with 2 material elements, and I try to change it to a mesh with 4 material elements, the first two are not updated, and the later 2 are set correctly.

I am setting the mesh this way

GetMesh()->SetSkeletalMesh(MeshToUse, true);

If I eject and check the mesh, it is correctly set, but if I check the materials, the outdated materials are called “MaterialInstanceDynamic_0” and so on, while the correct materials have their asset name.

This even happens from the editor without using any code. Say I eject, and change the mesh from the details panel. I get this same situation.

GetMesh()->EmptyOverrideMaterials(); seems to fix this, but I’m still investigating. I’m not sure if needed to do this is intended or a bug.

1 Like

Thanks, EmptyOverrideMaterials() IS working for me.
I had the same issue when changing hair mesh of my characters, the second hair mesh is using a different texture, but when I switched to it, Unreal was still using and displaying the first texture, so I got a wrong result. I added now the call to EmptyOverrideMaterials.

I needed to expose it to a BP as its not by default.

I did it with a library:

 UCLASS()
 class UBPUtils : public UBlueprintFunctionLibrary
 {
     GENERATED_BODY()

     UFUNCTION(BlueprintCallable, Category = "Mesh Assets")
     static void EmptyOverrideMaterials(UMeshComponent* mesh);
}

...

in the CPP:

  void UBPUtils::EmptyOverrideMaterials(UMeshComponent* mesh)
  {
      mesh->EmptyOverrideMaterials();
  }

what to do when this happens in the Blueprint interface? I’ve switched a mesh in an exisitng blueprint to a different mesh and the first material slot is applied on the mesh. It does this for all meshes, I’m adding a new material. no change