# CreateDefaultSubobject returns 0

**URL:** <https://forums.unrealengine.com/t/createdefaultsubobject-returns-0/474465>\
**Category:** Programming & Scripting\
**Tags:** unreal-engine\
**Created:** [November 14, 2020, 2:07pm UTC](https://forums.unrealengine.com/t/createdefaultsubobject-returns-0/474465 "2020-11-14T14:07:37Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![anonymous\_user\_e947fbe5](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_e947fbe5](https://forums.unrealengine.com/u/anonymous_user_e947fbe5)\
**Post date:** [November 14, 2020, 2:07pm UTC](https://forums.unrealengine.com/t/createdefaultsubobject-returns-0/474465/1 "2020-11-14T14:07:37Z")

</div>

PrimaryActorTick.bCanEverTick = true;  
PrimaryActorTick.bStartWithTickEnabled = true;  
this-\>AvatarComponent = CreateDefaultSubobject(TEXT(“AvatarComponent”));  
this-\>AddInstanceComponent(this-\>AvatarComponent);  
this-\>AttributeSet = CreateDefaultSubobject(TEXT(“AvatarAttributeSet”));  
if(this-\>AvatarComponent)  
{  
UE\_LOG(LogTemp, Warning, TEXT(“AvatarComponent is zero after Ctor!”))  
}

how may this code run into the print statementß? Any Suggestions on resolving?

---

<div class="post-metadata">

**Author:** ![Alekann01](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Alekann01](https://forums.unrealengine.com/u/Alekann01)\
**Post date:** [November 14, 2020, 2:34pm UTC](https://forums.unrealengine.com/t/createdefaultsubobject-returns-0/474465/2 "2020-11-14T14:34:34Z")

</div>

Hi Harqui

Create DefaultSubObject can only be called in the classes constructor as it will serialize the component so it can be used with the editor.

If you want to create the components at run time, you will need to spawn them and attach them.  
There are many similar questions to this.

> **[NewObject or CreateDefaultSubobject](https://forums.unrealengine.com/questions/317148/newobject-or-createdefaultsubobject.html)**
>
> Hi all, I’d like to know what is the difference between the 2 methods stated in the title. Example: USphereComponent\* Component = NewObject(this); or USphereComponent\* SphereComponent = CreateDefaultSubobject(TEXT("RootComponent")); Thank you.

> **[Can I dynamic CreateDefaultSubobject but not just in init function](https://forums.unrealengine.com/questions/104335/can-i-dynamic-createdefaultsubobject-but-not-just.html)**
>
> I want to dynamic create following object: TSubobjectPtr EmptySceneCom = aPCIP-\>CreateDefaultSubobject(this, newName); But if I did not create the object in init function like following: APositionIndicator::APositionIndicator(const...

Alex
