# OnCompoenentBeginOverlap.AddDynamic not working

**URL:** <https://forums.unrealengine.com/t/oncompoenentbeginoverlap-adddynamic-not-working/468365>\
**Category:** C++\
**Tags:** overlap-actors, attach-to-component, unreal-engine, CPP, question\
**Created:** [July 19, 2020, 1:45pm UTC](https://forums.unrealengine.com/t/oncompoenentbeginoverlap-adddynamic-not-working/468365 "2020-07-19T13:45:27Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Petr003](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Petr003](https://forums.unrealengine.com/u/Petr003)\
**Post date:** [July 19, 2020, 1:45pm UTC](https://forums.unrealengine.com/t/oncompoenentbeginoverlap-adddynamic-not-working/468365/1 "2020-07-19T13:45:27Z")

</div>

I want to make my player to pick up a weapon by letting it enter the sphere component of the weapon. If the overlap event is triggered, it should display an error message and pick up the weapon. But it is not displaying any message or picking up the weapon when I enter the sphere component.

cpp:

// Fill out your copyright notice in the Description page of Project Settings.

#include “Components/SceneComponent.h”

#include “weapon.h”

// Sets default values  
Aweapon::Aweapon(const FObjectInitializer& ObjectInitializer)  
: Super(ObjectInitializer)  
{  
// Set this actor to call Tick() every frame. You can turn this off to improve performance if you don’t need it.  
PrimaryActorTick.bCanEverTick = true;

```
RootComponent = CreateDefaultSubobject<USceneComponent>(TEXT("ROOT"));

MySphere = CreateDefaultSubobject<USphereComponent>(TEXT("sphere"));
MySphere->AttachToComponent(RootComponent, FAttachmentTransformRules::KeepRelativeTransform);

MyWeaponMesh = CreateDefaultSubobject<USkeletalMeshComponent>(TEXT("weapon_mesh"));
MyWeaponMesh->AttachToComponent(MySphere, FAttachmentTransformRules::KeepRelativeTransform);

DamageBox = CreateDefaultSubobject<UBoxComponent>(TEXT("BOX"));
DamageBox->AttachToComponent(MySphere, FAttachmentTransformRules::KeepRelativeTransform);

MySphere->OnComponentBeginOverlap.AddDynamic(this, &Aweapon::OnOverlapBegin);

```

}

// Called when the game starts or when spawned  
void Aweapon::BeginPlay()  
{  
Super::BeginPlay();

}

void Aweapon::OnOverlapBegin(class UPrimitiveComponent\* HitComp, class AActor\* OtherActor, class UPrimitiveComponent\* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult & SweepResult)  
{  
UE\_LOG(LogTemp,Error,TEXT(“OVERPLAP”))  
if (OtherActor) {  
APlayerCharacter\*player = Cast(OtherActor);  
if (player) {  
MySphere-\>AttachToComponent(player-\>GetMesh(),FAttachmentTransformRules::SnapToTargetNotIncludingScale,“weapon\_right”);  
}  
}

}

// Called every frame  
void Aweapon::Tick(float DeltaTime)  
{  
Super::Tick(DeltaTime);

}

* * *

.h:

// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include “CoreMinimal.h”  
#include “PlayerCharacter.h”  
#include “GameFramework/Actor.h”  
#include"Components/SphereComponent.h"  
#include"Components/BoxComponent.h"  
#include “weapon.generated.h”

UCLASS()  
class RPG\_PROJECT\_API Aweapon : public AActor  
{  
GENERATED\_BODY()

public:   
// Sets default values for this actor’s properties  
Aweapon(const FObjectInitializer& ObjectInitializer);

protected:  
// Called when the game starts or when spawned  
virtual void BeginPlay() override;

```
UPROPERTY(EditAnyWhere)
	USphereComponent* MySphere;

UPROPERTY(EditAnyWhere)
	USkeletalMeshComponent* MyWeaponMesh;

UPROPERTY(EditAnyWhere)
	UBoxComponent* DamageBox;
UFUNCTION()
	void OnOverlapBegin(class UPrimitiveComponent* HitComp, class AActor* OtherActor, class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult & SweepResult);

```

public:   
// Called every frame  
virtual void Tick(float DeltaTime) override;

};

---

<div class="post-metadata">

**Author:** ![EvilCleric](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@EvilCleric](https://forums.unrealengine.com/u/EvilCleric)\
**Post date:** [July 19, 2020, 1:51pm UTC](https://forums.unrealengine.com/t/oncompoenentbeginoverlap-adddynamic-not-working/468365/2 "2020-07-19T13:51:26Z")

</div>

Try putting the AddDynamic in the BeginPlay and not in the ctor:

```
// Called when the game starts or when spawned 
void Aweapon::BeginPlay() 
{ 
    Super::BeginPlay();
    MySphere->OnComponentBeginOverlap.AddDynamic(this, &Aweapon::OnOverlapBegin);
}
```
