Download

Add a Box Component at runtime to another actor

Hey, iam curently trying to bring my Blueprint “Code” over to C++. Iam struggeling about a … clearly simple problem.

Iam trying to add a BoxComponent to an Actor at runtime wich is not the addin Actor.

Cant get this thing to work… I have tried many variations of attaching the box to the new parent but none worked… maybe there is also a problem with my code… :frowning:


void ABrush_Master::CreateTraceBox(AActor* Parent, FVector Location, FVector Extents, FRotator Rotation, ECollisionChannel TraceChannel, bool Visivle, bool ArrowComponent)
{
	//No Valid Parent just exit
	if (!Parent) return;

	FTransform _Transform = FTransform(Rotation, Location, FVector(1, 1, 1));

	UBoxComponent* _BoxComponent = NewObject<UBoxComponent>(Parent);

	_BoxComponent->SetWorldTransform(_Transform);
	_BoxComponent->SetBoxExtent(Extents);
	_BoxComponent->SetVisibility(true);
	_BoxComponent->SetHiddenInGame(false);
	_BoxComponent->AttachParent = Parent->GetRootComponent();

	//Parent->AddOwnedComponent(_BoxComponent);
	//_BoxComponent->AttachTo(Parent->GetRootComponent());

}

You need to call _BoxComponent->RegisterComponent() once you’ve finished initializing it. I’m not sure if that is the only thing that is missing, but that is definitely going to help you.

if that will work… you are my personal hero of the day :slight_smile: