Dynamic Material Instance Changing All Occurances

In GridMaker.cpp

void AGridMaker::BeginPlay()
{
	Super::BeginPlay();
	
	
	GridArray.SetNum(10);
	for (int32 i = 0; i < GridArray.Num(); ++i)
	{
		GridArray[i].SetNum(10);
	}
	float GridXLocation = 0;
	float GridYLocation = 0;
	for (int32 i = 0; i < GridArray.Num(); ++i)
	{
		for (int32 j = 0; j < GridArray[i].Num(); ++j)
		{
			GridArray[i][j].Location = FVector(GridXLocation, GridYLocation, 10);
			if (UWorld* ThisWorld = GetWorld())
			{
				FVector ActorLocation = FVector(GridXLocation, GridYLocation, 0);
				GridArray[i][j].SetGridActor(Cast<AGridActor>(ThisWorld->SpawnActor(AGridActor::StaticClass(), &ActorLocation)));
				if (GridArray[i][j].GridActor)
				{
					
					if (LinkedMaterial)
					{
						UMaterialInstanceDynamic* Material_Dyn = UMaterialInstanceDynamic::Create(LinkedMaterial, NULL);
						GridArray[i][j].GridActor->SetDynamicMaterialInstance(Material_Dyn, FLinearColor::White);
						
					}										
				}
				
			}
			GridYLocation += 100.f;
		}
		GridXLocation += 100.f;
		GridYLocation = 0;
	}
}

also

bool AGridMaker::SetCellToRoad(int32 XCoordinate, int32 YCoordinate)
{
	if (XCoordinate >= 0 && YCoordinate >= 0 && XCoordinate < GridArray.Num() && YCoordinate < GridArray[XCoordinate].Num())
	{
		GEngine->AddOnScreenDebugMessage(1, 5, FColor::Black, FString::Printf(TEXT("Point is in range")));
		if (GridArray[XCoordinate][YCoordinate].GridActor)
		{
			GEngine->AddOnScreenDebugMessage(1, 5, FColor::Black, FString::Printf(TEXT("Actor is valid")));
			if (GridArray[XCoordinate][YCoordinate].GridActor->GetMarialInstance() != nullptr)
			{
				GEngine->AddOnScreenDebugMessage(1, 5, FColor::Black, FString::Printf(TEXT("Material is Valid")));
				UMaterialInstanceDynamic* Material_Dyn = UMaterialInstanceDynamic::Create(LinkedMaterial, NULL);
				GridArray[XCoordinate][YCoordinate].GridActor->SetDynamicMaterialInstance(Material_Dyn, FLinearColor::Green);
			}
		}
	}
	return false;
}

and in grid actor.cpp

void AGridActor::SetDynamicMaterialInstance(UMaterialInstanceDynamic* InMaterial, FLinearColor InColor)
{
	GridActorMeshComponent->GetStaticMesh()->SetMaterial(0, InMaterial);
	Material_Dyn = InMaterial;
	FLinearColor Color = InColor;
	Material_Dyn->SetVectorParameterValue(TEXT("Color"), Color);
}

for some reason, when i change color of one cell, it changes the color of all cells. why?

Solution. All new objects were using the same pointer to the same static mesh asset. To resolve this problem, i used DuplicateObject and saved that pointer to the gridactor static mesh. now each cell changes color independently.

updated code for reference
GridActorMeshComponent = CreateDefaultSubobject(“GridActorMeshComponent”);
auto TheCube = ConstructorHelpers::FObjectFinder(TEXT(“StaticMesh’/Engine/BasicShapes/Cube.Cube’”));

if (TheCube.Object)
{
if (UWorld* ThisWorld = GetWorld())
{
UStaticMesh* MyCube = DuplicateObject(TheCube.Object, this);
GridActorMeshComponent->SetStaticMesh(MyCube);
GridActorMeshComponent->SetWorldScale3D(FVector(.5));
}
}

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.