How to remove component runtime?

Hello so creating and adding a component to a actor at runtime was not that hard.
But i can not figuer out how remove a component at runtime.

All my searches turn up ::DestroyComponent() but that makes me crash the instant it`s called.
Can anyone tell the correct way of Removing a component from a actor at runtime?

Thanks

if the component was already destroyed and it tries to destroy it again during the loop, it will crash.

Try doing something like

if(component != NULL)
{

Destroy();   (or comopnent->Destroy(); )

}

Hope this helps!

1 Like

@spazchicken: the method Destroy() is not available on a component only actors.

I solved the issue how ever was very simple :wink:
I just call Component->UnregisterComponent();

1 Like

Actually UnregisterComponent and DestroyComponent both works for me in Editor.
But recreating new component with the same name crashes editor. Any way to fix this?
Called Garbage Collector but it does not help a lot.

Dynamic components should be created using NewObject, are you using that function? In that case, you can give the spawned component no name or set it to NAME_None. You shouldn’t run into the issue then.

Yes, I ended up passing NAME_None to components during creation. Still not sure if old components are correctly cleaned up. Hope they are.

for me calling ->UnregisterComponent(); would also crash Unreal… Please help

Hey Zhi Khang Shao,

I have a question, hope you know the answer, I create new UStaticMeshComponents at runtime and place them in an TArray. They are registered and i can succesfully set a mesh and manipulate their placement right after creation. However, if i try to access the same TArray, which was declared in the header file, at another time, the array is empty… the meshes are still visible in the editor, but for some reason the TArray is empty? any clue??? tahnks a billion!

Is this a final answer? Doesn’t look legit.

What I did is first UnregisterComponent() and then DestroyComponent(). This works for me

1 Like

just get children components and set to clear . It simplest way

Thank you really much, I wasted so many hours trying to figure this out!