# Add components dynamically

**URL:** <https://forums.unrealengine.com/t/add-components-dynamically/312401>\
**Category:** Programming & Scripting\
**Tags:** unreal-engine\
**Created:** [March 24, 2015, 4:25pm UTC](https://forums.unrealengine.com/t/add-components-dynamically/312401 "2015-03-24T16:25:09Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![anonymous\_user\_d9744154](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_d9744154](https://forums.unrealengine.com/u/anonymous_user_d9744154)\
**Post date:** [March 24, 2015, 4:25pm UTC](https://forums.unrealengine.com/t/add-components-dynamically/312401/1 "2015-03-24T16:25:09Z")

</div>

Hi everyone,

I am trying to create actors dynamically, and add to them components. But for now, it just crashes when I add a new component on my new actor…

Here is my code :

```
void AContentReader::CreateActor(const s_Content &data)
{
	if (GEngine)
		GEngine->AddOnScreenDebugMessage(-1, 2, FColor::Yellow, "Creating actor");
	FTransform tr = ConvertMatrixToTransform(data.matrix);
	_actor = GetWorld()->SpawnActor<AActor>(tr.GetLocation(), FRotator(tr.GetRotation()));
	if (!_actor)
	{
		if (GEngine)
			GEngine->AddOnScreenDebugMessage(-1, 2, FColor::Red, "Actor creation failed");
		return;
	}
	_parent = _actor->CreateDefaultSubobject<USceneComponent>(TEXT("Root")); // <-- This line makes crash
	return;
	_actor->SetRootComponent(_parent); //Not used for now
}

```

I have my new AActor, that’s fine, but it crashes when I use CreateDefaultSubobject.

I don’t really get what is happening. I am using CreateDefaultSubobject in another class in this project, and it works perfectly.

Should I call this function only in a constructor ? For now, my CreateActor function is call once, in a Tick. Is that the issue ?

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_d575feb6](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_d575feb6](https://forums.unrealengine.com/u/anonymous_user_d575feb6)\
**Post date:** [March 24, 2015, 4:38pm UTC](https://forums.unrealengine.com/t/add-components-dynamically/312401/2 "2015-03-24T16:38:12Z")

</div>

Hi,

Im useing this to spawn dynamic spline mesh components:

```
USplineMeshComponent* SplineMeshComp = ConstructObject<USplineMeshComponent>(USplineMeshComponent::StaticClass(), this);

```

I cant tell you why this should work and yours doesnt, but i can remember that i had problems with that, too.

Hope that helps…

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_d9744154](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_d9744154](https://forums.unrealengine.com/u/anonymous_user_d9744154)\
**Post date:** [March 24, 2015, 4:48pm UTC](https://forums.unrealengine.com/t/add-components-dynamically/312401/3 "2015-03-24T16:48:35Z")

</div>

Hi ,

Thanks for the answer ! Unfortunatelly, for me, it does’nt solve my problem, I still have the crash 😕  
I am wondering what I did wrong…

---

<div class="post-metadata">

**Author:** ![hyperdr1ve](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@hyperdr1ve](https://forums.unrealengine.com/u/hyperdr1ve)\
**Post date:** [March 24, 2015, 4:55pm UTC](https://forums.unrealengine.com/t/add-components-dynamically/312401/4 "2015-03-24T16:55:32Z")

</div>

You can only use `CreateDefaultSubobject` in the constructor. Why not just use SpawnActor and create the necessary components in the constructor of your actor?

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_fc4c82b6](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_fc4c82b6](https://forums.unrealengine.com/u/anonymous_user_fc4c82b6)\
**Post date:** [March 24, 2015, 5:21pm UTC](https://forums.unrealengine.com/t/add-components-dynamically/312401/5 "2015-03-24T17:21:19Z")

</div>

Hello,

could you be more specific about what type of crash happens and where it breaks? (LIke which breakpoint hits/ in what method)

You could try to give it another name. “Root” might give you a crash …

Good luck with that! 🙂

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_d9744154](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_d9744154](https://forums.unrealengine.com/u/anonymous_user_d9744154)\
**Post date:** [March 24, 2015, 9:20pm UTC](https://forums.unrealengine.com/t/add-components-dynamically/312401/6 "2015-03-24T21:20:13Z")

</div>

Ok thanks, that’s what I expected… I can’t create my components on the constructor of an object, cause I will add components through the game 😕

Any idea of how I could do that ? (if it’s possible…)

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_d9744154](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_d9744154](https://forums.unrealengine.com/u/anonymous_user_d9744154)\
**Post date:** [March 24, 2015, 9:31pm UTC](https://forums.unrealengine.com/t/add-components-dynamically/312401/7 "2015-03-24T21:31:53Z")

</div>

Hello,

The crash occurs in this line :

\_parent = \_actor-\>CreateDefaultSubobject(TEXT(“Root”));

The editor just freeze, and then the memory increase until it reach 100% of memory taken, or until I kill it ^^

I will not be able to change my code before friday, but i will test to change the name

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_fc4c82b6](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_fc4c82b6](https://forums.unrealengine.com/u/anonymous_user_fc4c82b6)\
**Post date:** [March 24, 2015, 9:37pm UTC](https://forums.unrealengine.com/t/add-components-dynamically/312401/8 "2015-03-24T21:37:28Z")

</div>

Ohhh and I didn’t read your code carefully enough!

You should use this:

```
USceneComponent* createdComp = ConstructObject<USceneComponent>(YourComponentsActualClass, this, YourComponentsAddedName);
if(createdComp)
{
	createdComp->RegisterComponent();
	createdComp->AttachTo(GetRootComponent(), NAME_None);
}

```

This also returns a reference to the newly constructed component 🙂

EDIT: I use this exact code and it works nicely 🙂

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_d9744154](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_d9744154](https://forums.unrealengine.com/u/anonymous_user_d9744154)\
**Post date:** [March 24, 2015, 9:43pm UTC](https://forums.unrealengine.com/t/add-components-dynamically/312401/9 "2015-03-24T21:43:41Z")

</div>

I have already tried that but it didn’t work 😕

I will test another time because maybe I missed something !

I will tell you on Friday.

Thanks for you answer 🙂

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_fc4c82b6](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_fc4c82b6](https://forums.unrealengine.com/u/anonymous_user_fc4c82b6)\
**Post date:** [March 24, 2015, 9:47pm UTC](https://forums.unrealengine.com/t/add-components-dynamically/312401/10 "2015-03-24T21:47:33Z")

</div>

Did you use RegisterComponent()? Yeah just try it and let me know, if there are any problems using that code 🙂

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_d9744154](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_d9744154](https://forums.unrealengine.com/u/anonymous_user_d9744154)\
**Post date:** [March 24, 2015, 9:48pm UTC](https://forums.unrealengine.com/t/add-components-dynamically/312401/11 "2015-03-24T21:48:30Z")

</div>

I didn’t so it may come from that ! 🙂

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_d9744154](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_d9744154](https://forums.unrealengine.com/u/anonymous_user_d9744154)\
**Post date:** [March 27, 2015, 9:01am UTC](https://forums.unrealengine.com/t/add-components-dynamically/312401/12 "2015-03-27T09:01:40Z")

</div>

It works perfectly ! 🙂  
Thanks a lot !

---

<div class="post-metadata">

**Author:** ![Jakey113G](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Jakey113G](https://forums.unrealengine.com/u/Jakey113G)\
**Post date:** [January 22, 2017, 10:21pm UTC](https://forums.unrealengine.com/t/add-components-dynamically/312401/13 "2017-01-22T22:21:23Z")

</div>

I’m seeing a lot of relevant answers but I want to point out that `ConstructObject` is deprecated (as of 4.8?!?), with a note saying to use NewObject instead.

You should use `NewObject` instead.  
such as

`NewObject< UStaticMeshComponent>(this);`

[Tutorial on Dynamical Constraint Creation](https://wiki.unrealengine.com/Physics_Constraints,_Create_New_Constraints_Dynamically_During_Runtime#C.2B.2B_for_Making_a_Dynamic_Physics_Constraint)

I added this as a follow up answer for future readers.

---

<div class="post-metadata">

**Author:** ![emperor\_katax](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/emperor_katax/32/1070178_2.png) [@emperor\_katax](https://forums.unrealengine.com/u/emperor_katax)\
**Post date:** [October 10, 2017, 3:44pm UTC](https://forums.unrealengine.com/t/add-components-dynamically/312401/14 "2017-10-10T15:44:34Z")

</div>

… here is my solution :

```
UE_LOG(LogTemp, Warning, TEXT(" ---- spawning ---- "));
AUnitElement* sample = GetWorld()->SpawnActor<AUnitElement>(AUnitElement::StaticClass(), FVector::ZeroVector, FRotator::ZeroRotator);
(sample->GetRootComponent())->AttachToComponent(ElementBox, FAttachmentTransformRules::SnapToTargetNotIncludingScale);
FVector temp = UKismetMathLibrary::RandomPointInBoundingBox(ElementBox->Bounds.Origin, ElementBox->Bounds.BoxExtent);
sample->SetActorLocation(temp);

```

AUnitElement\* is your “other” actor which has mesh inside.

ElementBox is just a container of UBoxComponet inside my class and is attached to the root

```
ElementBox = CreateDefaultSubobject<UBoxComponent>("Element Box");
RootComponent = ElementBox;

```

temp is a location vector made based on size of ElementBox .
