# Actor's Uproperty contains null pointer after begin BeginPlay

**URL:** <https://forums.unrealengine.com/t/actors-uproperty-contains-null-pointer-after-begin-beginplay/238353>\
**Category:** C++\
**Tags:** unreal-engine\
**Created:** [July 1, 2021, 5:06am UTC](https://forums.unrealengine.com/t/actors-uproperty-contains-null-pointer-after-begin-beginplay/238353 "2021-07-01T05:06:29Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Fenyx\_Invincible](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Fenyx\_Invincible](https://forums.unrealengine.com/u/Fenyx_Invincible)\
**Post date:** [July 1, 2021, 5:06am UTC](https://forums.unrealengine.com/t/actors-uproperty-contains-null-pointer-after-begin-beginplay/238353/1 "2021-07-01T05:06:29Z")

</div>

HI there! I’m new in UE and C++ in general (btw have a lot experience in Java). I have issues with null pointers when I add my character to editor and start playing. I created several logs of actor initialization and it confused me even more.

TacticalCharacter.h

```auto
class ATacticalCharacter : public ACharacter
{
	GENERATED_BODY()
public:
	ATacticalCharacter();

	UPROPERTY(EditDefaultsOnly, BlueprintReadWrite, Category = "Attributes")
	UCharacterAttributes* Attributes;
	...

```

TacticalCharacter.cpp

```auto
ATacticalCharacter::ATacticalCharacter()
{
	Attributes = NewObject<UCharacterAttributes>(this, TEXT("Attributes"));
	Attributes->Name = GetName();
	UE_LOG(LogTemp, Warning, TEXT("Actor created %s Attributes is set %d"), *(Attributes->Name), (Attributes)?1:0);
	assert(Attributes);
}

```

CharacterAttributes.h

```auto
UCLASS()
class UCharacterAttributes : public UActorComponent
{
	GENERATED_BODY()
public:
	FString Name;
	virtual void BeginDestroy() override;

```

Also I create Blueprint from TacticalCharacter  
When I start dragging TacticalCharacter Blueprint to map in editor I have following in the logs:

LogTemp: Warning: Actor created TacticalCharacterPlayer\_BP\_C\_3 Attributes is set 1  
LogTemp: Warning: Actor created TacticalCharacterPlayer\_BP\_C\_4 Attributes is set 1

When I press Play and put breakpoint to line: Attributes = NewObject(this, TEXT(“Attributes”));  
I can see properly initialized object for actor with name TacticalCharacterPlayer\_BP\_C\_5 (why 5?)  
Next breakpoint in UCharacterAttributes ::BeginDestroy. I can see that Attributes is destoryed only for TacticalCharacterPlayer\_BP\_C\_3

Next breakpoint in TacticalCharacter::BeginPlay where Attributes is null now.  
Why this happens? Why is object created so many times? I haven’t seen that Attributes for TacticalCharacterPlayer\_BP\_C\_5 was destroyed (at least BeginDestroy was not called)

Will be appreciated for any help

---

<div class="post-metadata">

**Author:** ![LXFrontier](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@LXFrontier](https://forums.unrealengine.com/u/LXFrontier)\
**Post date:** [July 1, 2021, 7:53am UTC](https://forums.unrealengine.com/t/actors-uproperty-contains-null-pointer-after-begin-beginplay/238353/2 "2021-07-01T07:53:40Z")

</div>

You shouldn’t create objects in your constructor. Try using BeginPlay() or PostInitializeComponents().

> **[Actor Lifecycle](https://docs.unrealengine.com/4.26/en-US/ProgrammingAndScripting/ProgrammingWithCPP/UnrealArchitecture/Actors/ActorLifecycle/)**
>
> What actually happens when an Actor is loaded or spawned, and eventually dies.

What happens during object construction in UE4 is a whole mystical process to me, and there are tons of threads of people trying to understand it. If you want, I suggest you look into Unreal’s reflection system, how CDOs work, and look into threads trying to demystify the whole process.  
Or just use BeginPlay() and leave it for another day.

AActor::OnConstruction() is the native version of the construct script in blueprints, but this case doesn’t seem to want that.

I kept this bookmarked since I was confused by this when I was starting out as well:

> [@UPROPERTY member vars reset to NULL by ObjectInitializer](https://forums.unrealengine.com/t/uproperty-member-vars-reset-to-null-by-objectinitializer/19959/11):
>
> The biggest thing that stands out is this: //StatsComponent.cpp UStatsComponent::UStatsComponent(const class FObjectInitializer& ObjectInitializer) : Super(ObjectInitializer) { Stats = \*\*NewObject\<URPGStats\>(this)\*\*; check(Stats); //always valid } Objects constructed in this fashion will not have the proper flags set that mark it as a subobject for serialization. I suspect what is happening here is that the constructor is invoked, then in ~FObjectInitializer, property values are serialized…

UObjects shouldn’t have a specifier of the “Edit” variety either. You should be using VisibleDefaultsOnly.

> **[Property Specifiers](https://docs.unrealengine.com/4.26/en-US/ProgrammingAndScripting/GameplayArchitecture/Properties/Specifiers/)**
>
> Keywords used when declaring UProperties to specify how the property behaves with various aspects of the Engine and Editor.

This video goes through it nicely too.

[![](https://d3kjluh73b9h9o.cloudfront.net/original/4X/4/7/e/47e2d05ce3497f6dc4c022de8756d1941c865549.jpeg "Unreal Engine 4 - UPROPERTY Explained [Breakdown Series]") ](https://www.youtube.com/watch?v=ZPJtFa9srXw)

---

<div class="post-metadata">

**Author:** ![Fenyx\_Invincible](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Fenyx\_Invincible](https://forums.unrealengine.com/u/Fenyx_Invincible)\
**Post date:** [July 1, 2021, 10:57am UTC](https://forums.unrealengine.com/t/actors-uproperty-contains-null-pointer-after-begin-beginplay/238353/3 "2021-07-01T10:57:31Z")

</div>

Thx a lot for reply. I don’t understand why so many tutorials are using constructor to initialize properties. Also in my case a lot of properties work fine, except mine created one. For example

```auto
FullPathSpline = CreateDefaultSubobject<USplineComponent>(TEXT("FullPathSpline"));
AddedSplineMeshComponents.Empty();
CaptureComponentHead = CreateDefaultSubobject<USceneCaptureComponent2D>(TEXT("CaptureComponentHead"));
CaptureComponentHead->CaptureSource = SCS_FinalColorLDR;
CaptureComponentHead->bCaptureEveryFrame = false;	
	
FString asd = GetName();
UTextureRenderTarget2D* renderTarget = NewObject<UTextureRenderTarget2D>(this, *asd);
renderTarget->InitAutoFormat(512, 512);
renderTarget->UpdateResourceImmediate();
CaptureComponentHead->TextureTarget = renderTarget;
CaptureComponentHead->UpdateContent();

```

All of these properties work fine. Although I have issues now with USceneCaptureComponent2D. If I create UTextureRenderTarget2D in constructor if works, but UE uses same instance for all actors. If I move it to BeginPlay, I have separate UTextureRenderTarget2D for each actor but nothing works… I have only black image in that case.

As I understand from lifecicle PostInitializeComponents is more proper place to init objects, because **BeginPlay** - Called when the level is started and for complex init I would have fps lose on startup, right?

PS  
I’ve tryed to move

```auto
CaptureComponentHead = CreateDefaultSubobject<USceneCaptureComponent2D>(TEXT("CaptureComponentHead"));

```

to PostInitializeComponents and BeginPlay and catch the error

```auto
Fatal error: [File:D:/Build/++UE4/Sync/Engine/Source/Runtime/CoreUObject/Private/UObject/Obj.cpp] [Line: 112] No object initializer found during construction.

UE4Editor_CoreUObject
UE4Editor_Tactical!ATacticalCharacterPlayer::PostInitializeComponents() [D:\Games\Tactical\Source\Tactical\TacticalCharacterPlayer.cpp:82]

```

When in case of cunstructor all works fine. I don’t understand what should I move to constructor and to PostInitializeComponents/BeginPlay. What is the rule to take this decision )

---

<div class="post-metadata">

**Author:** ![LXFrontier](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@LXFrontier](https://forums.unrealengine.com/u/LXFrontier)\
**Post date:** [July 1, 2021, 11:06am UTC](https://forums.unrealengine.com/t/actors-uproperty-contains-null-pointer-after-begin-beginplay/238353/4 "2021-07-01T11:06:07Z")

</div>

PostInitializeComponents() and BeginPlay() are both called when the game is started so both will hit performance the same. I wouldn’t worry about that unless you’re doing something incredibly long or have a ton of instances created all at the same time.

You should use CreateDefaultSubobject() in the constructor, not BeginPlay(). Components are usually created in the constructor, objects that should be spawned in editor go into OnConstruction() (such as spawning mesh along a spline to create pipes in a level), and all other objects should be created at runtime such as BeginPlay().
