# Attach child collider from code to actor

**URL:** <https://forums.unrealengine.com/t/attach-child-collider-from-code-to-actor/339783>\
**Category:** World Creation\
**Tags:** collision-detection, unreal-engine, Collision, skeletalmesh, CPP, question\
**Created:** [November 26, 2015, 8:12pm UTC](https://forums.unrealengine.com/t/attach-child-collider-from-code-to-actor/339783 "2015-11-26T20:12:03Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![anonymous\_user\_ce8e7eb6](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_ce8e7eb6](https://forums.unrealengine.com/u/anonymous_user_ce8e7eb6)\
**Post date:** [November 26, 2015, 8:12pm UTC](https://forums.unrealengine.com/t/attach-child-collider-from-code-to-actor/339783/1 "2015-11-26T20:12:03Z")

</div>

Hi!

I would like to add a simple sphere collider to my actor as a child element, all from code.

My actor’s root is a SkeletalMesh, that’s the visual representation, the sphere is the Physical one.

```
//header
USphereComponent* Collider;
USkeletalMeshComponent* MeshComponent;

```

I’ve managed to add the child sphere element, but I can’t seem to find a way to make it react to gravity and collision.

I could only make it happen with the root , mesh component, but then colliding body is not the sphere’s, but the visual mesh of my actor.

```
//cpp, constructor
MeshComponent	= CreateDefaultSubobject<USkeletalMeshComponent>(TEXT("MeshComponent"));
MeshComponent->SetSimulatePhysics(true);
MeshComponent->SetCollisionResponseToAllChannels(ECollisionResponse::ECR_Block);
MeshComponent->SetCollisionEnabled(ECollisionEnabled::QueryAndPhysics);

Collider = CreateDefaultSubobject<USphereComponent>(TEXT("Collider"));
Collider->SetRelativeLocation(FVector(0.0f, 0.0f, 0.0f));
Collider->AttachTo(MeshComponent);

```

I also want to listen to event’s when my character passes through the object.

How to achieve this from c++?
