Change mesh material instantaneously

When I change the material of a mesh using UStaticMeshComponent::SetMaterial, it turns gray for two seconds before displaying the new material.
Is there a way to make the transition instantaneous, or a workaround?

The only solution I can think of is having a clone with the other material already applied, and swap them. But I would rather avoid doing that.
I am not asking for dynamic parameters, this is a completely new material.

I use SetMaterial in a very systematic fashion in my project (basically every mesh component gets it) and it’s not supposed to do that.

What may be happening is that the material isn’t compiled - if that’s the case you’ll be fine in a release game. But you should get the error only once.
The other possibility is that the material isn’t loaded, and there is no easy solution.

The best way to handle such a case is to use material instances. Set the parent material to your object, and then you can apply instances of that material instantly.

It doesn’t do it anymore after rebuilding my project from scratch. The material is a dynamic instance with a bunch of static switches. It could be related to that, or I made a mistake somewhere, hard to tell now.
Good to know that this is a common thing to do.

Well I wouldn’t say it’s common, but it’s definitely supposed to work.

Another thing I thought of : are you using LODs ? SetMaterial() can be tricky to work with if you’ve got multiple LODs, multiple material indexes, or both.

No LOD or multiple materials for now, but that will change.
Is it very expensive to iterate through all the LOD and indexes and change their materials?

It shouldn’t be expensive at all. A material is just an attribute of the geometry, changing it is straightforward.

I did have a few issues with the combination of LOD and material indexes, I ended up extending StaticMeshComponent and parsing the StaticMesh->RenderData->LODResources array that contains all LODs, and then the Sections array inside that LOD unit that contains materials. I do that at the component’s instanciation, store the material instance, and then update it at will.

That will be useful in the future, thanks.