Adding Actor Components at runtime

Hello! Ar runtime, I’m creating
AStaticMeshActor* PlacedLadder.
I need to attach a ULadder component to it at runtime. Is it possible? I saw the function AddComponent(), but I don’t know how to use it. I am quite new to the UE4 so please anwser clearly.

Hi there! Are you using BP or C++?

I’m using C++

If you’re running this from inside the actor you want to add the component to:

ULadderComponent* Ladder = NewObject<ULadderComponent>(this, ULadderComponent::StaticClass(), NAME_None, RF_Transient);
Ladder->SetupAttachment(ComponentToAttachTo);
Ladder->RegisterComponent();
3 Likes

Thanks! That helped a lot!