Search code examples
c++unreal-engine4

UE4: Set texture to material (C++)


I'm new to unreal coding, I want to apply texture to material, but I'm stuck with creating:

    UMaterialInterface* Material = CreateDefaultSubobject<UMaterial>(TEXT("MyMaterial"));
    UMaterialInstanceDynamic* DynamicMatierial = UMaterialInstanceDynamic::Create(Material, this);

I've got this error message:

NewObject with empty name can't be used to create default subobjects (inside of UObject derived class constructor) as it produces inconsistent object names. Use ObjectInitializer.CreateDefaultSuobject<> instead.

This error comes from second line. Any idea why this error occurs? Or is there any other easy way to apply texture to material?


Solution

  • After 4 days of searching I've found correct answer to my texture problem in this post:

    https://forums.unrealengine.com/development-discussion/c-gameplay-programming/46406-how-to-initialize-umaterial-and-assign-texture-to-it#post466817