Question
How to SetMaterial on UStaticMeshComponent?
Description
I was able to SetStaticMesh without any problem, but SetMaterial seems not working correctly. It changes internal Name returned by GetFullName and Blueprint default value, but seems has no visual effect in rendering.
Code
Here is what I’ve tried:
public: UPROPERTY(BlueprintReadWrite, VisibleAnywhere, Category = "Components",
SimpleDisplay, meta = (ExposeOnSpawn = true, DisplayName = "HitBox"
)) UStaticMeshComponent* HitBox;
(test is assert, readonly is empty, info makes log)
// HitBox
HitBox = CreateDefaultSubobject<UStaticMeshComponent>("HitBox");
if(test(HitBox))[[likely]]{
// Mesh
{
static const ConstructorHelpers::FObjectFinder<UStaticMesh> MeshAsset(
TEXT("StaticMesh'/Engine/BasicShapes/Cylinder'")
);
if(test(MeshAsset.Succeeded()))[[likely]]{
readonly UStaticMesh* const Mesh = MeshAsset.Object;
if(test(Mesh))[[likely]]{
HitBox->SetStaticMesh(Mesh);
};
};
};
// Material
{
static const ConstructorHelpers::FObjectFinder<UMaterial> MaterialAsset(
TEXT("Material'/Engine/BasicShapes/BasicShapeMaterial'")
);
if(test(MaterialAsset.Succeeded()))[[likely]]{
readonly UMaterial* const Material = MaterialAsset.Object;
if(test(Material))[[likely]]{
info("Old Material: %s", *HitBox->GetMaterial(0)->GetFullName());
HitBox->SetMaterial(0, Material);
info("New Material: %s", *HitBox->GetMaterial(0)->GetFullName());
};
};
};
SetRootComponent(HitBox);
};
Logs
Logs confirms that path value is changed internally, but no visual effect is applied:
[2018.08.31-16.16.17:013][676]Log: APlayer0::APlayer0@Player0.cpp:39; Old Material: Material /Engine/EngineMaterials/DefaultMaterial.DefaultMaterial
[2018.08.31-16.16.17:013][676]Log: APlayer0::APlayer0@Player0.cpp:42; New Material: Material /Engine/BasicShapes/BasicShapeMaterial.BasicShapeMaterial
Blueprint
Static Mesh above is correctly set and rendered, but Materials[0] is not updated correctly.
PS Version: 4.19.2-4033788+++UE4+Release-4.19