Spawning actor, and setting mesh

Hi,

I have ATile class (based on AActor): Tile.h:



UCLASS()
class ENDLESSRUNNER_API ATile : public AActor
{
GENERATED_BODY()

public:
// Sets default values for this actor's properties
ATile();

UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Mesh)
class UStaticMeshComponent* m_MeshComponent;

protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;

public:
// Called every frame
virtual void Tick(float DeltaTime) override;

UFUNCTION()
void SetMesh(class UStaticMesh* mesh);

UFUNCTION()
FVector GetBounds();
};

and Tile.CPP



ATile::ATile()
{
// 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;

RootComponent = CreateDefaultSubobject<USceneComponent>(TEXT("RootComponent"));
m_MeshComponent = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("MeshComponent"));
if (m_MeshComponent)
{
m_MeshComponent->SetupAttachment(GetRootComponent());
UE_LOG(LogTemp, Warning, TEXT("TILE: MeshComponent found, Set to RootComponent"))
}

}

// Called when the game starts or when spawned
void ATile::BeginPlay()
{
Super::BeginPlay();
}

// Called every frame
void ATile::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);

FString DebugMsg = FString::Printf(TEXT("MeshComponent: %s"), *m_MeshComponent->GetStaticMesh()->GetName()); // THIS LINE WILL CAUSE TROUBLE!!!
GEngine->AddOnScreenDebugMessage(4, 0.f, FColor::Blue , DebugMsg);
}


void ATile::SetMesh(UStaticMesh* mesh)
{
FString meshName = mesh->GetName();
UE_LOG(LogTemp, Warning, TEXT("MeshName: %s"), *meshName)

if (m_MeshComponent)
{
static ConstructorHelpers::FObjectFinder<UStaticMesh> BaseMeshAsset(TEXT("StaticMesh'/Engine/MapTemplates/SM_Template_Map_Floor.SM_Template_Map_Floor'"));
m_MeshComponent->SetStaticMesh(BaseMeshAsset.Object);
UE_LOG(LogTemp, Warning, TEXT("TILE: SetMesh"))
}
}


In my game manager i spawn the tiles like this:



AActor* AGameManager::SpawnTile(FVector loc, UStaticMesh* mesh)
{
FRotator Rotation(0.f, 0.0f, 0.f);
FActorSpawnParameters SpawnInfo;

ATile* SpawnedActorRef = GetWorld()->SpawnActor<ATile>(m_TileToSpawn, loc, Rotation, SpawnInfo);
SpawnedActorRef->SetMesh(m_TileMeshArray[0]);

return SpawnedActorRef;
}


The problem is, that m_MeshComponent are set to nullptr after it has been spawned (the ATile() has been called, via SpawnActor())
What is the reason for that ?

Could someone please give med a hint :slight_smile:
Thx.
​​​​​​​