Actor component is null in BeginPlay, Tick

Why might this be occurring? In another actor it’s fine, in this and another it’s not.

ABuildingActor::ABuildingActor()
{
	PrimaryActorTick.bCanEverTick = true;

	 = CreateDefaultSubobject<USceneComponent>(TEXT("Root"));

	MapElement = CreateDefaultSubobject<UMiniMapElement>(TEXT("MapElement"));
	MapElement->Mobility = EComponentMobility::Static;
	MapElement->Layer = EMiniMapLayer::ML_Buildings;
	MapElement->bIsVisibleToOtherTeam = false;
	MapElement->TeamName = this->TeamName;
}

void ABuildingActor::BeginPlay()
{
	Super::BeginPlay();

	// TODO: For some reason MapElement is nullptr here
	UEmpiresFunctionLibrary::GetGameMode(this)->GetTeam(TeamName)->Map->AddElement(MapElement);
}

Try Adding:

MapElement = CreateDefaultSubobject<UMiniMapElement>(TEXT("MapElement"));
MapElement->SetupAttachment();

...

I thought that too, but UMiniMapElement is a UActorComponent, not a USceneComponent so it doesn’t have attachment. What’s weirder is that in the editor, I can see the element in the list on the actor.

I see…

You might want to do something like this, to get a better understanding where that call is falling apart (I am guessing how your classes are setup so you’ll need to fill in some gaps):

if(MapElement)
{
	UEmpiresGameModeBase* GameMode = UEmpiresFunctionLibrary::GetGameMode(this);
	if(GameMode)
	{
		TArray<UEmpiresTeam*> Team = GameMode->GetTeam(TeamName);
		if(Team.Num( ) > 0)
		{
			Team->Map->AddElement(MapElement);
		}
		else
		{
			UE_LOG(LogTemp, Warning, TEXT("Failed to get team back from GameMode");
		}
	}
	else
	{
		UE_LOG(LogTemp, Warning, TEXT("Failed to get game mode!");
	}
}
else
{
	UE_LOG(LogTemp, Warning, TEXT("Map element is nullptr.");
}