If the object you are setting the overlay on has been changed in some ways, you might need to reset it first.
I don’t know if you are using bp or c++, but this is what I have. It runs a lot of checks, but essentially ensures I will never have a crash. Should also be easy to replicate in BP.
void ADungeonBuilding::Highlight(const bool Highlight)
{
if (Highlight)
{
if (OverlayMat->IsValidLowLevel())
{
if (!IsValid(BuildingMesh))
{
TArray<UStaticMeshComponent*> StaticMeshItems;
GetComponents(StaticMeshItems);
if (!StaticMeshItems.IsEmpty())
{
BuildingMesh = StaticMeshItems[0];
BuildingMesh->SetIsReplicated(true);
}
else
{
UE_LOG(LogTemp, Warning, TEXT("DungeonBuilding::Highlight -> No mesh was found!"));
return;
}
}
BuildingMesh->SetOverlayMaterial(OverlayMat);
}
else
{
UE_LOG(LogTemp, Warning, TEXT("DungeonBuilding::Highlight -> No overlay material was found!"));
}
}
else
{
if (IsValid(BuildingMesh))
{
if (EmptyOverlayMat->IsValidLowLevel())
{
BuildingMesh->SetOverlayMaterial(EmptyOverlayMat);
}
else
{
UE_LOG(LogTemp, Warning, TEXT("DungeonBuilding::Highlight -> No EmptyOverlayMat found on deselect! (1)"));
}
}
else
{
BuildingMesh = GetComponentByClass<UStaticMeshComponent>();
if (IsValid(BuildingMesh))
{
BuildingMesh->SetIsReplicated(true);
if (EmptyOverlayMat->IsValidLowLevel())
{
BuildingMesh->SetOverlayMaterial(EmptyOverlayMat);
}
else
{
UE_LOG(LogTemp, Warning, TEXT("DungeonBuilding::Highlight -> No EmptyOverlayMat found on deselect! (2)"));
}
}
else
{
UE_LOG(LogTemp, Warning, TEXT("DungeonBuilding::Highlight -> No mesh found on deselect!"));
}
}
}
}