Cannot attach actors without Root Components

Hello.

I’m an experienced Unity developer who recently wished to switch to Unreal, but I am having trouble getting to understand the basics with all my Unity3d mindset.

I’ve created a simple class just following this tutorial: https://docs.unrealengine.com/latest/INT/Programming/QuickStart/7/index.html

So I have added the class to the scene and there is a connection. But I want to try and manipulate simple objects in the scene, like moving them around and such, using code. So in Unity I would just drag a primitive box underneath my GameObject containing my class. But when I try to do that in Unreal, I get this: e53ab9b91ea8250dc8f35002d8c19b1ee3118c64c3.png

How do I get a “Root Component”? And how do I attach my class to that, and put primitive objects underneath it?

With regards

1 Like

An Actor without a root component has no position.

If you are building an Actor through blueprints, it has a “dummy” Scene Component by default that exists to give it a position. You can add other components below that, or replace it as the root component.

In the C++ case, the default Actor does not provide this root component. In your class constructor you will need to create some form of Scene Component and assign it to the RootComponent variable. The simplest form of this would be:

RootComponent = PCIP.CreateDefaultSubobject<USceneComponent>(this, TEXT(“SceneComponent”));

If you wanted to add a Mesh component you could instead create that. Looking at StaticMeshActor.cpp should give some insight in how to do this for a more complex (and visual) component type.

If your goal is to build up a number of related static meshes, or what not in a hierarchy, I’d probably recommend looking at building that out in blueprints rather than C++ as it is much easier to make proper asset references and relationships in your hierarchy there.

5 Likes

+1 to Marc Audy for his amazingly detailed answer!

:heart:

Rama

Thank you, I will try and look into it. Regarding your last statement about Blueprint, I guess I am just the kind of programmer that’s scared of WYSIWYG stuff, but everybody talks so good about Blueprint so I should probably look into it instead.

Just in case anyone comes here in the future this actually doesn’t work as written here. It has been changed, at least in my “current” version of 4.5.1.

Here is the correct syntax to use:

.h file:


	UPROPERTY()
	TSubobjectPtr<class USceneComponent> DummyRoot;

.cpp file:


ANEWImpactEffect::ANEWImpactEffect(const class FPostConstructInitializeProperties& PCIP) : Super(PCIP)
{
	//Default root component needed for C++ classes, Blueprints create one on the fly
	DummyRoot = PCIP.CreateDefaultSubobject<USceneComponent>(this, TEXT("RootComponent"));
	RootComponent = DummyRoot;
}

I encountered this issue when my ImpactEffects were always spawning at the origin (V(0) or 0,0,0). Blueprints automatically create a dummy component but there isn’t one in C++. So this will fill that need.

Thanks for helping me get in the right direction though (so many months later)!

2 Likes

Just for the sake of keeping this up to date, as this still fixes the problem described by Kory.

Edit: “Current” version 4.19

In the constructor of your actor:


RootComponent = CreateDefaultSubobject<USceneComponent>(TEXT("RootSceneComponent"));

1 Like