The thing is that the “DefaultSubobject” is a method with some extra functionality related to the Editor property setup functionality (at least that’s what I use it for). That is: use that method if you want to get the settings you entered at the editor entity inspector.
I usually create my ordinary objects dynamically with something like this:
Don’t forget to do
var->RegisterObject();
after NewObject
I just lost a hour or 2 because my skeletal mesh wasn’t visible, nor ticking, but the component (gun) worked, and correctly shooted on click
I couldn’t understand why ._.
Using Unreal 5.5, used RegisterComponent instead of RegisterObject and just created the actual component instead of casting to parent USceneComponent:
UInstancedStaticMeshComponent* Ism = NewObject<UInstancedStaticMeshComponent>(this,UInstancedStaticMeshComponent::StaticClass()); // per @darkgaze comment
Ism->RegisterComponent(); // needed so component is visible, per @cpu comment
FString ISMName = FName("ISM_Mesh_").ToString().Append(FString::FromInt(1));
Ism->Rename(*ISMName);
// IsmParent is a USceneComponent parent that has Ism child
Ism->AttachToComponent(IsmParent, FAttachmentTransformRules::KeepRelativeTransform);