I am trying to dynamically create and attach a component to my custom component. The issue is the component created does not show in editor, specifically the component window.
As shown in this image, a box collision component is created and attached to UPrimitiveEffectComponent(a custom component I created), but it is not visible in component window.
The following method works for me when attaching components to actors dynamically in the editor (tested on 4.25.4). However to actually see the component section updated with the new components you need to refresh the Details panel (exempli gratia by selecting another actor on the map and then selecting again the modified actor).
auto const comp = NewObject<UMyComponent>(this, FName{TEXT("MyComponent")});
if (!comp ) { UE_LOG(LogTemp, Error, TEXT("Error creating component")); }
// Magic line. Needed in order to show the component on Component section of Details in the editor
comp->CreationMethod = EComponentCreationMethod::Instance;
if (!comp->AttachToComponent(parent, FAttachmentTransformRules::SnapToTargetIncludingScale, TEXT("MySocket")))
{
UE_LOG(LogTemp, Error, TEXT("Impossible to attach the component to the parent"));
}
comp->RegisterComponent();
AddInstanceComponent() will set the CreationMethod to EComponentCreationMethod::Instance for you
As previously noted, this will not update the SCSEditor embedded in the details panel.
To avoid having to manually click out and back in, use the following Editor only code to trigger a refresh in PostEditChangeProperty() on your component.
Since this introduces a dependency on GEditor you should add the following snippet in your .build.cs for the module to include UnrealEd in editor builds.
I built a whole hierarchy of classes and editor tools for level creation and spent dozens of hours over a better part of a week testing it to track down a little bug that drove me insane.