Material Instance Base Color Changing

Hi all

I am trying to change the base color of a material. Here is the bp

and the error it gives is

I assume it is something related to PieceMeshMaterialDynamic. It is from ChessPiece class.

inside header

    UPROPERTY(EditDefaultsOnly, Category = "Chessia")
    UStaticMeshComponent * PieceMesh;

    UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Chessia")
    UMaterialInstanceDynamic * PieceMeshMaterialDynamic;

and inside source

    PieceMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("SM_Piece"));

    PieceMeshMaterialDynamic = PieceMesh->CreateDynamicMaterialInstance(0, PieceMesh->GetMaterial(0));

Now i don’t know much, and this is my first time doing a material instancing inside c++. I just couldn’t figure out where the problem lies. My best guess is, the problem lies at the line which i initialize PieceMeshMaterialInstance.

How can i solve this problem? Thanks in advance

Just like i guest it, PieceMeshMaterialDynamic initialization fails. It is always null.

Still have to figure out the correct way to do it.

I just found out that i need to use CreateAndSetMaterialInstance function. Also you need to carry material instance creation inside PreInitializeComponents() function.After these you are good to go :slight_smile: