Access to RootComponent not available


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:

class UBoxComponent* m_RootComponent;


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)

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 ?


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)