Component made with NewObject cannot be moved

I’m using NewObject to create object that rotate arount it’s parent.

UMySceneComponent* Orbiter = NewObject<UMySceneComponent>(this->GetOwner(), TEXT("OrbigintSubBall"));
        Orbiter->RegisterComponentWithWorld(GetWorld());
        Orbiter->RegisterComponent();

The trouble is that if object is created via attaching in editor, everything is fine. When i’m using NewObject, that thing doesn’t move. UMySceneComponent derive from USceneComponent and moving in a tick function by just setting a local position. What’s wrong?

Thats because you did not attach it to anything :stuck_out_tongue_winking_eye: or at least you left out the Code that would tell me otherwise.

https://docs.unrealengine.com/latest/INT/Programming/Tutorials/Components/1/

Orbiter->AttachTo(this->GetOwner()->GetRootComponent(), NAME_None, EAttachLocation::SnapToTargetIncludingScale);
or

Orbiter->SetupAttachment(this->GetOwner()->GetRootComponent());

does literally nothing

Can you Upload a bit more Code to www.pastebin.com .h/.cpp and post it here? Would also like to See your Tick Function and how you refer to your Orbiter. Since your Code suggest to me that you don´t store a ref and get a grasp to your Component dynamicly somehow.

OrbitingObjectSpawnorComponent.cpp : OrbitingObjectSpawnorComponent.cpp - Pastebin.com

OrbitingObjectSpawnorComponent.h : OrbitingObjectSpawnorComponent.h - Pastebin.com

MySceneComponent.cpp : MySceneComponent.cpp - Pastebin.com

MySceneComponent.h : MySceneComponent.h - Pastebin.com

Hmm Played around a bit. By the looks of it your Component Ticks correctly and does what you expect. The Problematic Part is that it somehow fails to attach your Mesh Comnponent as Child even thoug it shows up in the Hierarchy. Im a little confused myself at this Point. Usually you Create Components from the Owning Actor itself and not via Components.

Tried a coupe ways now restricting myself to Create the Mesh Component from the construction Script. Troublesome xD (btw. you dont need to create a seperate USceneComponent you can use a StaticMeshComponent Directly instead and do your Tick Calculations there)

Oh and by the way if you just want to make it work remove all the stuff you got in the Constructor and do it via Begin Play like this:

void UOrbitingObjectSpawnorComponent::BeginPlay()
{
	Super::BeginPlay();

	if (this->GetOwner() && this->GetOwner()->GetRootComponent())
	{
		UMySceneComponent* Orbiter = NewObject<UMySceneComponent>(this->GetOwner(), TEXT("OrbigintSubBall"));
		Orbiter->SetupAttachment(this->GetOwner()->GetRootComponent());//does nothing
		//Orbiter->RegisterComponentWithWorld(GetWorld());
		UStaticMeshComponent* Visual = NewObject<UStaticMeshComponent>(this->GetOwner(), TEXT("Visual"));
		UStaticMesh* TheMesh = LoadObject<UStaticMesh>(GetOuter(), TEXT("StaticMesh'/Game/Geometry/Meshes/1M_Cube_Chamfer.1M_Cube_Chamfer'"));
		Visual->SetStaticMesh(TheMesh);
		Visual->SetupAttachment(Orbiter);
		Orbiter->RegisterComponent();
		Visual->RegisterComponent();
	}
}

Result:

Seems like I figured out what’s wrong. Moved all the “ball creation” stuff into “BeginPlay” instead of constructor and now everything is fine. Seems like if my call stack doesn’t belong anymore to “usual” constructor call stack, things can get messy. Lost few hours figuring out what’s wrong, but thx for an idea.

xD Posted you the Solution right bellow. Guess you figured it out by yourself with the Hint to the Construction script.