Unreal Engine 5 · Blueprint → C++
Add a Component at Runtime in UE5 C++UE Docs
Adding a component dynamically maps to NewObject<T>(), followed by RegisterComponent() and AttachToComponent(). This is the runtime counterpart to creating components in the constructor.
Blueprint node & C++ equivalent
UStaticMeshComponent* Mesh = NewObject<UStaticMeshComponent>(this);
Mesh->RegisterComponent();
Mesh->AttachToComponent(RootComponent, FAttachmentTransformRules::KeepRelativeTransform);The C++ equivalent
Create the component instance with NewObject<UStaticMeshComponent>(this), passing the owning Actor as the outer. Call Mesh->RegisterComponent() so the engine starts ticking and rendering it. Finally attach it with Mesh->AttachToComponent(RootComponent, FAttachmentTransformRules::KeepRelativeTransform) to place it in the component hierarchy.
Without RegisterComponent, the component exists but is never initialized into the world.
When to use it in C++
Use this pattern when the number or type of components is not known at compile time, such as procedurally spawning meshes or gameplay-driven attachments. For fixed components known at design time, prefer CreateDefaultSubobject in the constructor.
FAttachmentTransformRules::KeepRelativeTransform keeps the component's current relative transform; other rules let you snap to the target or keep world transform.
Frequently asked questions
How do I add a component at runtime in UE5 C++?+
Use NewObject<UStaticMeshComponent>(this), then call RegisterComponent(), then AttachToComponent to add it to the hierarchy.
Why do I need to call RegisterComponent?+
A component created with NewObject is not active until registered. RegisterComponent() initializes it so it renders, ticks, and participates in collision.