tried this for UActorComponent and didn’t get result for both skinned and static mesh
UActorComponent* NewComp = NewObject (UStaticMeshComponent,ownerActor, name);
but got it with your method by dividing into too functions, THANKS!
Hi, I have tried that, work fine (creates the components and registers it) but it doesnt ever show that component in the editors panel (in v.4.21.2). Do you know any way to make it visible?
hello am from future, in 4.26.2 the following was done during ::PreInitializeComponents() to create a mesh component thats also visible in properties panel, added some comments for google’s sake:
USkeletalMeshComponent* NewSkeletalMeshComponent = NewObject<USkeletalMeshComponent>(this, USkeletalMeshComponent::StaticClass(), NewMeshCompName, RF_NoFlags, FoundSkeletalMeshComponent); //take note of last argument here, it almost makes a perfect copy of FoundSkeletalMeshComponent, including scale and USkeletalMesh
NewSkeletalMeshComponent->SetupAttachment(GetRootComponent());
NewSkeletalMeshComponent->RegisterComponent();
//NewSkeletalMeshComponent->AttachToComponent(args) this will crash the game, and its redundant as RegisterComponent does an AttachToComponent using data from SetupAttachment!!!
AddInstanceComponent(NewSkeletalMeshComponent); //without this, component wont show up in properties
I also noticed, that if you omit AddInstanceComponent not only will components not show in the outline, they will also not be saved if you save. For context: I was trying to add components to an actor during edit time by button press on an editor exposed function that would attach some components. After adding the function call to AddInstanceComponent, saving in editor would finally also save the newly added components. However, it would still not mark my actor dirty. So I recommend everyone who is doing something similar to call Parent->Modify() as a final step.