Access to RootComponent not available

Hello Guys,

That’s my first Topic in this forum. I started Unreal a few weeks ago but I have some C++ Knowledge. I want to create a class InteractableObejcts, where some other Objects inherit from. I Create with CreateDefaultSubobject an BoxComponent and set this to RootComponent. After that I want access to the RootComponent with GetRootComponent() but it’s every time nullptr. Maybe I do anything wrong, but I saw the same code in Unreal Source Code Character.h/cpp and if I create a class which inherits from Character.h I can access the CapsuleComponent which is the RootComponent of the Character with GetRootComponent(). I didn’t figure out what I do wrong. Is it because it’s Unreal API Code and not the Game Code? If you need more Informations, please ask. I read a lot the last 2 Days in the forum but didn’t find an answer to my question.

Edit: It seems like the RootComponent doesn’t store the BoxComponent as it’s RootComponent. In Scope of the Constructor the RootComponent is right assigned as the BoxComponent but in other methods the RootComponent is nullptr.

Edit: I figured out that the problem is that the RootComponent gets deleted while optimization. I don’t know why Unreal does this but how can I avoid this?

Sry for the edits, I want to work and I’m annoyed about this problem because it cost me so much time.


You could just make a function called GetBoxComponent(). You can’t use GetRootComponent() because that returns a USceneComponent but your root is a UBoxComponent. Its an inheritance problem. You are trying to access a derived class using a base class getter which is not possible. Its not an Unreal issue but c++ polymorphism issue (object slicing). I would suggest making a getter for the box component.

I thought the same but I went to the Unreal Engine Source Code and saw the Character.h from Unreal, they do the same. It inherits from AActor class, then they Set the CapsuleComponent as RootComponent and if I want to access the RootComponent I can easily use GetRootComponent() in my own .cpp file which inherits from Character.h. And they only use USceneComponent* as return type for GetRootComponent. So it have to work I think.

I think you can just add a Scene Component and make it a root. Now you can access it via GetRootComponent (it can be anything Scene Component which sits at the top).

If I add a Scene Component as Root Component and want to add a force to the Object I can’t add it to the Scene Component and if I add an extra ShapeComponent which has the addForce Function only the ShapeComponent gets the force and move. But the attached Mesh don’t move with the extra ShapeComponent.
So without an ShapeComponent as RootComponent I can’t add a force to the whole object.

Edit: Ninja_K, the problem isn’t only that I don’t want to do it like that. If I can’t access the RootComponent in methods like ComponentBeginOverlap, i have to get the class of the Object, cast it to the class I expect to get from the Overlap. It’s rly extensive and a lot of issues can happen.

Edit: Ok I found out, that I can access the RootComponent with GetRootComponent in Tick and in OnOverlapBegin methods, but not in other methods. So my problem is almost solved. It’s ok when I can’t access it anywhere else.

Thanks everybody for your answers