Download

Create & attach component to parent Actor from child Component?

Hello !
I’m trying to make a CameraManager component, which would automatically create and attach a SpringArm and a Camera to its parent Actor.
I got some answers from here thread but no luck so far.

I tried

USpringArmComponent* SpringArmCT = NewObject(GetOwner(), USpringArmComponent::StaticClass(), “SpringArmCT”);
if (SpringArmCT)
{
SpringArmCT->RegisterComponent();
SpringArmCT->AttachTo(GetOwner()->GetRootComponent());
}

but it doesn’t work, the component doesn’t show up at runtime.

I tried SpringArmCT->OnComponentCreated(), SpringArmCT->InitializeComponent(), but it still doesn’t work :frowning:

Any help pls ? thanks :slight_smile:

Edit : I also tried

USpringArmComponent* SpringArmCT = NewObject(GetOwner(), TEXT(“SpringArmCT”));
if (SpringArmCT)
{
SpringArmCT->SetupAttachment(GetOwner()->GetRootComponent());
SpringArmCT->RegisterComponent();
}

and some other variants lol … nothing works. And btw I call this from the component BeginPlay.

:frowning:

So I tried to write the same code in the parent Actor BeginPlay…

SpringArmCT = NewObject<USpringArmComponent>(this, TEXT("SpringArmCT"));
if (SpringArmCT)
{
	UE_LOG(LogTemp, Warning, TEXT("Comp Added ?"));
	SpringArmCT->SetupAttachment(RootComponent);
	SpringArmCT->RegisterComponent();
}

… and it works. I’m missing something.

Please someone help !