How do I change the material

When I spawn a pawn how do I change the material:

AMyPawn* pawn = GetWorld()->SpawnActor<AMyPawn>(PawnList[p], spawnLocation, spawnRotation);

for (TObjectIterator<UStaticMeshComponent> Itr(pawn->GetWorld()); Itr; ++Itr)
    Itr->SetMaterial(0, PawnList[p].GetDefaultObject()->Materials[m]);

Hello ,

Here is a small code snippet. You can try to use it for changing material in your pawn.



class MYPROJECT3_API AMyPawn : public APawn


	UStaticMeshComponent* StaticMeshComponent;
	UStaticMeshComponent* GetStaticMeshComponent() { return StaticMeshComponent; }



	Init your StaticMeshComponent

Changing the material on pawn’s static mesh:

AMyPawn* pawn = GetWorld()->SpawnActor<AMyPawn>(AMyPawn::StaticClass(), SpawnLocation, SpawnRotation);
pawn->GetStaticMeshComponent()->SetMaterial(0, Material);

Material - your UMaterial*

Hope this helps.

Thank you for the response.

I’m using another implementation:

    TArray<UMaterialInterface*> Materials;

    const TArray<UActorComponent*>& theComponents = pawn->GetComponents();
    int32 componentCount = theComponents.Num();
    for (int32 x = 0; x < componentCount; x++)
        USkeletalMeshComponent* mesh = Cast<USkeletalMeshComponent>(theComponents[x]);
        if (mesh)
            mesh->SetMaterial(0, MyPawn.GetDefaultObject()->Materials[m]);

It appears that pawns with a skeletal mesh are handled differently.