Why Material Set Texture does not work??

I have a UTexture2D Object.When I Set the texture on the material, I find it does not show my texture.
The material is blank, but the texture param has data.

Anyone Can help me.

my code:

void ADataSourceActor::ChangeMaterial()
{
	//UTexture2D* pTmpTexture = Cast<UTexture2D>(StaticLoadObject(UTexture2D::StaticClass(), NULL, TEXT("Texture2D'/Game/Materials/t.t'")));
	UTexture2D* pTmpTexture = GetTexture2DFromDiskFile(TEXT("D:\\tex.jpg"));
	if (NULL == pTmpTexture)
	{
		return;
	}

	UMaterialInterface* pMasterMaterialRef = /*UMaterial::GetDefaultMaterial(MD_Surface);*/m_pRuntimeMesh->GetMaterial(0);
	if (NULL == pMasterMaterialRef)
	{
		return;
	}

	TArray<UTexture*> OutTextures;
	pMasterMaterialRef->GetUsedTextures(OutTextures, EMaterialQualityLevel::Num, false, ERHIFeatureLevel::Num, false);
	
	UMaterialInstanceDynamic* pRV_MatInst = UMaterialInstanceDynamic::Create(pMasterMaterialRef, this);
	if (NULL == pRV_MatInst)
	{
		return;
	}

	pRV_MatInst->SetTextureParameterValue(FName("MainTexture"), pTmpTexture);
	m_pRuntimeMesh->SetMaterial(0, pRV_MatInst);
}

look at this

Hello Dinglw,

Was my answer at the other post you linked able to help you out with this? If not, do you have any questions in particular that I may be able to help out with? From the screenshot it seems like it is being set to me, as neither of those look blank, unless you’re talking about the Physical Material?

Your answer is very helpful to me!
According to your method, I met a new problem.
I created a material in the package, like this

Then I create two actot.
ADataSourceActor::ADataSourceActor()
{
// Set this actor to call Tick() every frame. You can turn this off to improve performance if you don’t need it.
PrimaryActorTick.bCanEverTick = true;

	m_pRuntimeMesh = CreateDefaultSubobject<URuntimeMeshComponent>("MeshData");
	RootComponent = m_pRuntimeMesh;

	static ConstructorHelpers::FObjectFinder<UMaterialInterface> Material(TEXT("Material'/Game/Materials/TexMaterial.TexMaterial'"));
	m_pMasterMaterialRef = Material.Object;
	m_pRuntimeMesh->SetMaterial(0, m_pMasterMaterialRef);
}

After create, I set the material.

void ADataSourceActor::ChangeMaterial(const FString& strFileName)
{
	UTexture2D* pTmpTexture = GetTexture2DFromDiskFile(strFileName);
	if (NULL == m_pMasterMaterialRef || NULL == pTmpTexture)
	{
		return;
	}

	UMaterialInstanceDynamic* pRV_MatInst = UMaterialInstanceDynamic::Create(m_pMasterMaterialRef, this);
	if (NULL == pRV_MatInst)
	{
		return;
	}
	pRV_MatInst->SetTextureParameterValue(FName("BaseColor"), pTmpTexture);
	m_pRuntimeMesh->SetMaterial(0, pRV_MatInst);
}

But the second actor’s texture unable to show.

I check the material on the actor. But I can’t find any problem.
The material on the actor1

The material on the actor2

Can you help me to solve this problem?

Thank you!

Where/when are you calling the ChangeMaterial function? This could be related to why this isn’t working. Please show the code for the function where it’s being called.

void ASpawnActorGameMode::CreateActor()
{
FString strDataFile = FPaths::GameContentDir() + “MeshData.xml”;

	TArray<MESH_DATA> arrMeshData;
	ReadActorData(strDataFile, arrMeshData);

	for (int i = 0; i < arrMeshData.Num(); i++)
	{
		// Create Actor
		ADataSourceActor *pActor = (ADataSourceActor*)GWorld->SpawnActor(ADataSourceActor::StaticClass());
		if (NULL == pActor)
		{
			return;
		}
		pActor->SpawnActorInGame(i, arrMeshData[i]);

		// Set Material
		pActor->ChangeMaterial("D:\\t.jpg");
		m_arrActors.Add(pActor);
	}
}

I ChangeMaterial after create actor

Are these two actors of the same class or is it a different class? It seems that you’re hardcoding the texture asset that you’re assigning. Are you sure that the path that you’re providing for the second actor’s texture is correct?

Two objects are created by the same class.
There is no problem of the determination of the image path disk.
Is not the same as the pictures of the reason is, I have to turn the code and let it loads the different files

I found the reason.
Because I repeat to create the material.

Thank you very much for your patience to help!