Trouble moving components created using NewObject<>()

Hi I’m working on a project in C++ right now where I am trying to create my own building system that the player can use similar to others like Fortnite or ARK as a practice experiment. The trouble I have run into is I am trying to create an array of box components at 4 sockets on a mesh to use for snapping building pieces together. I have successfully created the box components and the player is able to cast a line trace that hits them however, the boxes all appear on the root location of the mesh rather than on the 4 corners which is where the sockets are placed.

The code I am using to create the box components is as follows:

The commented out line is the other method I tried to place the box component on top of the socket locations but got the same result as the currently compiled line.

This usually happens because the boxes are created at runtime but never actually attached/registered with the socket, so their relative transform stays at the root. With NewObject(this) you need to do the steps that CreateDefaultSubobject + the constructor normally handle for you: call Box->SetupAttachment(MeshComp, SocketName) (or Box->AttachToComponent(MeshComp, FAttachmentTransformRules::SnapToTargetNotIncludingScale, SocketName) if the mesh is already registered), then Box->RegisterComponent(), and optionally AddInstanceComponent(Box) so it shows in the details panel. Also make sure you do this in BeginPlay or OnConstruction rather than the constructor, because socket transforms aren’t valid until the static mesh is assigned and the component is registered. If you used GetSocketLocation() and SetWorldLocation() before RegisterComponent, the location gets overwritten when it registers, which matches what you’re seeing. A quick sanity check is MeshComp->DoesSocketExist(SocketName) to rule out a typo in the socket name.

The above comment is more or less accurate, but I believe the way you’re doing it is also mostly fine – just adding a call to RegisterComponent before the attach call should suffice, I believe.