Access to RootComponent not available

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.

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)

image