Set Material to a mesh, material is always NULL

Hi,

I’m trying to add a material to a UStaticMeshComponent in C++. I created a blueprint out of the C++ class and I selected a material. I searched the forum and found similar questions, but the answers are saying the same think I did already. So I think I did everything right, but my debug message still says “No Material”.
This is my code:
BaseBuilding.h


UPROPERTY(EditDefaultsOnly)
	UStaticMeshComponent* BuildingMesh;

//The material to use
	UPROPERTY(EditAnywhere, BlueprintReadWrite)
	class UMaterialInterface* Material;

BaseBuilding.cpp


ABaseBuilding::ABaseBuilding(const FObjectInitializer &ObjectInitializer) : Super(ObjectInitializer)
{
...]
	if (Material != NULL)
	{
		if (GEngine)
			GEngine->AddOnScreenDebugMessage(-1, 2.f, FColor::Green, FString::Printf(TEXT("Setting Material")));
		BuildingMesh->SetMaterial(0, Material);
	}
	else
	{
		if (GEngine)
			GEngine->AddOnScreenDebugMessage(-1, 2.f, FColor::Red, FString::Printf(TEXT("No Material")));
	}
}

I hope someone can help me with this.

No on an idea what I’m doing wrong? Are there missing infos to solve the problem?

Your material should be declared :


UPROPERTY(EditAnywhere, BlueprintReadWrite)
UMaterial* Material;

When you create the BaseBuilding, (i.e. when the constructor is called) the Material will not be assigned. You need to assign the material in BP, before you assign the material to the mesh.

Thank you! I moved it away from the constructor into another function that is called from my game and now it works!