Download

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]);
}


Maybe this one can help you.
https://answers.unrealengine.com/questions/40158/how-can-i-access-project-materials-from-code.html

Thank you 3dgo :slight_smile:

Is there a simpler way:



.h
    UPROPERTY(EditAnywhere)
    TArray<UMaterialInterface*> Materials;


.cpp
    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]);
        }
    }