# Setting RootComponent in C++ constructor doesn't actually change the root component

**URL:** <https://forums.unrealengine.com/t/setting-rootcomponent-in-c-constructor-doesnt-actually-change-the-root-component/300764>\
**Category:** C++\
**Tags:** root-component, unreal-engine, Blueprint, CPP, question\
**Created:** [December 6, 2014, 1:16pm UTC](https://forums.unrealengine.com/t/setting-rootcomponent-in-c-constructor-doesnt-actually-change-the-root-component/300764 "2014-12-06T13:16:36Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![rajkosto](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@rajkosto](https://forums.unrealengine.com/u/rajkosto)\
**Post date:** [December 6, 2014, 1:16pm UTC](https://forums.unrealengine.com/t/setting-rootcomponent-in-c-constructor-doesnt-actually-change-the-root-component/300764/1 "2014-12-06T13:16:36Z")

</div>

The root component (at least when viewing in the components tab of a derived BP) is always the subobject i created first in the constructor with FObjectInitializer::CreateDefaultSubobject. Changing the order of these around changes what the root component ends up being. All that setting RootComponent variable or using SetRootComponent method in the constructor does is remove the dummy ‘Root Component’ that shows up in the blueprint components tab when you forget to set it.

engine 4.6.0

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_b666e59e](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_b666e59e](https://forums.unrealengine.com/u/anonymous_user_b666e59e)\
**Post date:** [December 16, 2014, 8:37pm UTC](https://forums.unrealengine.com/t/setting-rootcomponent-in-c-constructor-doesnt-actually-change-the-root-component/300764/2 "2014-12-16T20:37:27Z")

</div>

Hi ,

Sorry for not getting back to you on this issue sooner. I was able to reproduce the results that you described, and I have submitted a report about this issue to have it investigated further (UE-6769).

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_b5d25213](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_b5d25213](https://forums.unrealengine.com/u/anonymous_user_b5d25213)\
**Post date:** [September 15, 2017, 7:54am UTC](https://forums.unrealengine.com/t/setting-rootcomponent-in-c-constructor-doesnt-actually-change-the-root-component/300764/3 "2017-09-15T07:54:27Z")

</div>

Is this fixed?

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_b666e59e](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_b666e59e](https://forums.unrealengine.com/u/anonymous_user_b666e59e)\
**Post date:** [September 15, 2017, 3:38pm UTC](https://forums.unrealengine.com/t/setting-rootcomponent-in-c-constructor-doesnt-actually-change-the-root-component/300764/4 "2017-09-15T15:38:04Z")

</div>

Hi SuKim,

This issue was fixed a while ago. If you are seeing this happen again, please create a new post on AnswerHub with details about what you are seeing and the steps that you are taking to get those results.

---

<div class="post-metadata">

**Author:** ![najmm](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@najmm](https://forums.unrealengine.com/u/najmm)\
**Post date:** [November 25, 2022, 5:08am UTC](https://forums.unrealengine.com/t/setting-rootcomponent-in-c-constructor-doesnt-actually-change-the-root-component/300764/5 "2022-11-25T05:08:18Z")

</div>

This still happens in UE 5.1.  
The first component created using CreateDefaultSubObject becomes the RootComponent automatically.

---

<div class="post-metadata">

**Author:** ![thedistantdreame](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@thedistantdreame](https://forums.unrealengine.com/u/thedistantdreame)\
**Post date:** [May 10, 2024, 11:47am UTC](https://forums.unrealengine.com/t/setting-rootcomponent-in-c-constructor-doesnt-actually-change-the-root-component/300764/6 "2024-05-10T11:47:05Z")

</div>

Have the same problem in 5.4

 ![Снимок экрана 2024-05-10 164757](https://d3kjluh73b9h9o.cloudfront.net/original/4X/1/5/4/15434c4555dc5e29175b68246bd644249a6f77aa.png)  
 ![Снимок экрана 2024-05-10 164645](https://d3kjluh73b9h9o.cloudfront.net/original/4X/f/1/4/f1487db89a370708028f5ef180b3fbc286a26c39.png)

```auto
ABuilding::ABuilding()
{
	this -> SetActorEnableCollision(false);
	InstancedMeshComponent = CreateDefaultSubobject<UInstancedStaticMeshComponent>(TEXT("InstancedMeshComponent"));
	StaticMeshComponent = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("StaticMeshComponent"));
	
	if(InstancedMeshComponent)
	{
		InstancedMeshComponent -> SetCollisionEnabled(ECollisionEnabled::NoCollision);
	}

	
	if (BuildingType == EBuildingType::FixedSize)
	{
		SetRootComponent(StaticMeshComponent);
	}
	else
	{
		SetRootComponent(InstancedMeshComponent);
		
	}

}

void ABuilding::BeginPlay()
{
	if (InstancedMeshComponent && InstancedMeshComponent->GetInstanceCount() > 0)
	{
		InstancedMeshComponent->ClearInstances();
	}
	
	if (BuildingType == EBuildingType::FixedSize)
	{
		SetRootComponent(StaticMeshComponent);
	}
	else
	{
		SetRootComponent(InstancedMeshComponent);
		
	}
}

```
