I basically create various forms out of cube static mesh using instances in UInstancedStaticMeshComponent. But SetMaterial does not work, unlike when it comes to UStaticMeshComponent.
Well it kinda works - in that it does show in the editor window as set upon hitting play, but it isn’t shown visually.
I don’t need anything complicated, like setting material per instance, I just need to change material for whole actor/component globally and programmatically so every instance has the same material.
Don’t quote me, but from memory you need to get the static mesh from the component and then get/set the material rather than directly on the instanced mesh component.
apologies I realised you’re probably talking c++. Same concept though
Well if you want to do that you also need material instance. You just set the material on the mesh, but then the material can be changed from C++ instantly since it’s dynamic once you set the material on the mesh.
“I don’t need anything complicated, like setting material per instance” Well you do, there is no other way to change things while game is running.
Changing materials on the static mesh itself doesn’t work either - at least during runtime. The cube (aka static mesh) shows having the material in the editor properties, but is still drawn as “default texture grey” in the viewport.
Worth noting that I spawn actors programmatically as well and material needs to be applied after “begin play”. I did try creating a UMaterialInstanceDynamic out of UMaterial and then applying it to both the mesh and mesh component (alone each or together) - but the result is the same - it shows as having the correct looking dynamic material instance in the properties - but doesn’t show in the viewport.
In this case material applied only to static instanced mesh component, but applying it to static mesh underneath also shows up as having “worldgrid” material in the viewport, while the mesh has the material applied in the properties
Maybe you forgot to write a parameter to the material that is tied up to c++
First you need to define a parameter in the material node for your material. Then you need to set a Value for Texture Parameter in c++ with the same parameter name. It’s an FString name.
At the very end you need to Set Material at the end in c++ as in setting the texture on the material.
Then after that in the editor you just load the material onto the slot and it should work, the slot should display the material dynamic instance with a number.
You may want to format the texture your self in C++ because this way you get direct values in c++ on it and you tie it to the instance you have there in the same CPP page, but it may work without this part.
You have to enable the Usage in the material (“Used with Instanced Static Meshes”)
Without this option the material with be replaced by the default grey material