# Access to RootComponent not available

**URL:** <https://forums.unrealengine.com/t/access-to-rootcomponent-not-available/408736>\
**Category:** C++\
**Tags:** nullptr, unreal-engine, question, CPP\
**Created:** [November 4, 2017, 2:03pm UTC](https://forums.unrealengine.com/t/access-to-rootcomponent-not-available/408736 "2017-11-04T14:03:49Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Josai](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Josai](https://forums.unrealengine.com/u/Josai)\
**Post date:** [November 4, 2017, 2:03pm UTC](https://forums.unrealengine.com/t/access-to-rootcomponent-not-available/408736/1 "2017-11-04T14:03:49Z")

</div>

Hello,

I have a Problem with the RootComponent of a class. I try to Get Access of the RootComponent of an AActor with GetRootComponent. Before I tried this I set a BoxComponent as RootComponent. But if I want to get access of the Root I ever get a nullptr. How can I access the RootComponent, because I want to add a force to the object but it doesn’t work without access to the Root.

Short Example Code:  
Test.h

class UBoxComponent\* m\_RootComponent;

Test.cpp  
Constructor:

m\_RootComponent = CreateDefaultSubobject(“RootComponent”);

//some other Stuff to configure the RootComponent

RootComponent = m\_RootComponent;

Other Method:

UBoxComponent\* box = GetRootComponent(); // I tried this, because the Root is a box  
USceneComponent\* root = GetRootComponent(); //I tried this, because the output of GetRootComponent is a USceneComponent

OtherTestClass.cpp (inherits from Test.h)

Method:  
UBoxComponent\* box = GetRootComponent();  
USceneComponent\* root = GetRootComponent();

The methods under the Constructor are my tries to get access to the RootComponent, but every try gives me an error with a nullptr. How can i fix it to add a force to this actor ?

Josai.

---

<div class="post-metadata">

**Author:** ![darkgaze](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@darkgaze](https://forums.unrealengine.com/u/darkgaze)\
**Post date:** [March 5, 2018, 10:56am UTC](https://forums.unrealengine.com/t/access-to-rootcomponent-not-available/408736/2 "2018-03-05T10:56:44Z")

</div>

these methods are available if you are inside a ScenComponent.  
In the constructor method:

```
if (parentActor != NULL)
	parentActor->SetRootComponent(this); // This is needed because this is also executed when opening the editor is null.

```

Somewhere else:

```
m_RootComponent = CreateDefaultSubobject("RootComponent");
if (actor != NULL && m_RootComponent != NULL)
     actor->SetRootComponent(m_RootComponent)

```

---

<div class="post-metadata">

**Author:** ![phil123456](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@phil123456](https://forums.unrealengine.com/u/phil123456)\
**Post date:** [April 26, 2023, 4:46pm UTC](https://forums.unrealengine.com/t/access-to-rootcomponent-not-available/408736/3 "2023-04-26T16:46:18Z")

</div>

> [@darkgaze](#):
>
> ```auto
> if (parentActor != NULL)
> parentActor->SetRootComponent(this); // This is needed because this is also executed when opening the editor is null.
> 
> ```

![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/c/b/4/cb4bb31c55260e82ab0d6a7aaa4fc18704363e60.png)
