Setting SamplerType crashes when creating new material from code


I’d like to make custom material from editor, everything works perfectly fine except the one thing - Setting SamplerType crashes editor with assertion:

Assertion failed: TextureReferenceIndex != INDEX_NONE [File:D:\Build\++UE4\Sync\Engine\Source\Runtime\Engine\Private\Materials/HLSLMaterialTranslator.h] [Line: 3992] Material expression called Compiler->TextureParameter() without implementing UMaterialExpression::GetReferencedTexture properly

Full stacktrace: SamplerType_Stacktrace -

Short reproduced code:

void ABugCoder::CreateMaterial()
	UPackage* Package = CreatePackage(NULL, TEXT("/Game/SomeFolder/M_MyMaterial"));
	UPackage* OutermostPkg = Package->GetOutermost();
	UMaterialFactoryNew* Factory = NewObject<UMaterialFactoryNew>();
	UMaterial* Mat = Cast<UMaterial>(Factory->FactoryCreateNew(UMaterial::StaticClass(), OutermostPkg, TEXT("M_MyMaterial"), RF_Standalone | RF_Public, NULL, GWarn));

	UMaterialExpressionTextureSampleParameter2D* NormalTexture = NewObject<UMaterialExpressionTextureSampleParameter2D>(Mat);
	NormalTexture->Texture = LoadObject<UTexture2D>(NULL, TEXT("/Engine/EngineMaterials/FlatNormal"), NULL, LOAD_None, NULL);
	NormalTexture->SamplerType = EMaterialSamplerType::SAMPLERTYPE_Normal; // Line that will affect in crash in PostEditChange(); 
    // it works without this line, but need to set SamplerType manually (from material editor) in created material
	Mat->Normal.Connect(0, NormalTexture);



We’ve recently made a switch to a new bug reporting method using a more structured form. Please visit the link below for more details and report the issue using the new Bug Submission Form. Feel free to continue to use this thread for community discussion around the issue.


I;ve submited it. Got an reply “it’s not a bug”, but no further answer.
It’s still an issue and I’ve completely no idea how to solve it