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.
MyPawn.h
...
UCLASS()
class MYPROJECT3_API AMyPawn : public APawn
{
...
public:
...
UStaticMeshComponent* StaticMeshComponent;
UStaticMeshComponent* GetStaticMeshComponent() { return StaticMeshComponent; }
...
};
MyPawn.cpp
AMyPawn::AMyPawn()
{
...
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:
.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]);
}
}
It appears that pawns with a skeletal mesh are handled differently.