# Actor component is null in BeginPlay, Tick

**URL:** <https://forums.unrealengine.com/t/actor-component-is-null-in-beginplay-tick/403996>\
**Category:** C++\
**Tags:** unreal-engine, question, CPP\
**Created:** [September 7, 2017, 1:07pm UTC](https://forums.unrealengine.com/t/actor-component-is-null-in-beginplay-tick/403996 "2017-09-07T13:07:15Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![duke22](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@duke22](https://forums.unrealengine.com/u/duke22)\
**Post date:** [September 7, 2017, 1:07pm UTC](https://forums.unrealengine.com/t/actor-component-is-null-in-beginplay-tick/403996/1 "2017-09-07T13:07:15Z")

</div>

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

```

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_61b4a355](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_61b4a355](https://forums.unrealengine.com/u/anonymous_user_61b4a355)\
**Post date:** [September 7, 2017, 2:59pm UTC](https://forums.unrealengine.com/t/actor-component-is-null-in-beginplay-tick/403996/2 "2017-09-07T14:59:19Z")

</div>

Try Adding:

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

...

```

---

<div class="post-metadata">

**Author:** ![duke22](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@duke22](https://forums.unrealengine.com/u/duke22)\
**Post date:** [September 8, 2017, 3:06am UTC](https://forums.unrealengine.com/t/actor-component-is-null-in-beginplay-tick/403996/3 "2017-09-08T03:06:02Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_61b4a355](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_61b4a355](https://forums.unrealengine.com/u/anonymous_user_61b4a355)\
**Post date:** [September 8, 2017, 3:52pm UTC](https://forums.unrealengine.com/t/actor-component-is-null-in-beginplay-tick/403996/4 "2017-09-08T15:52:18Z")

</div>

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

```
