New component not appearing in actor details

Posting and answering this myself so it can help wayward souls elsewhere.

Imagine you want to add a new component to your actor at runtime. You would have to use the following code:

UPrimitiveComponent* AChosenActor::CreateSphereVolume()
{
	USphereComponent* newSphere = NewObject<USphereComponent>(this, TEXT("NewSphere1"));
	newSphere->RegisterComponent();

	newSphere->AttachToComponent(this->RootComponent, FAttachmentTransformRules(EAttachmentRule::KeepRelative, false));

}

But when previewing your actor in the editor, you can see that the sphere is spawned but it does not appear in the actor’s Details panel. This means you can’t view or edit your new component. Bummer!

1 Like

You need to call AddInstanceComponent on your actor so it becomes aware of the new component.

UPrimitiveComponent* ATargetFinder::CreateSphereVolume(const FTargetFinderFactoryStruct& data)
{
	USphereComponent* newSphere = NewObject<USphereComponent>(this, TEXT("DebugSphere1"));
	newSphere->RegisterComponent();

	newSphere->AttachToComponent(this->RootComponent, FAttachmentTransformRules(EAttachmentRule::KeepRelative, false));

	// IMPORTANT.  Ensures the actor knows about the component and it is visible in the details panel.
	this->AddInstanceComponent(newSphere);

  // Do whatever else here...
1 Like