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

Tile.cpp:

// Sets default values
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;


}

// Called when the game starts or when spawned
void ATile::BeginPlay()
{
	Super::BeginPlay();
	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 every frame
void ATile::Tick(float DeltaTime)
{
	Super::Tick(DeltaTime);

}


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"))
	}
}

I spawn the actor in a “game manager”, and after that uses the Tile->SetMesh() Method.
But the problem is, that after the actor has been spawned, the m_MeshComponent seems do be dereferenced again.

Code for the “SpawnTile” looks 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;
}

Could you please give me a “hint” :slight_smile: