# C++ Collision detection not triggering

**URL:** <https://forums.unrealengine.com/t/c-collision-detection-not-triggering/311065>\
**Category:** World Creation\
**Tags:** unreal-engine, Physics, Collision, UE4, CPP, question\
**Created:** [March 15, 2015, 12:19pm UTC](https://forums.unrealengine.com/t/c-collision-detection-not-triggering/311065 "2015-03-15T12:19:28Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![phischphood](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@phischphood](https://forums.unrealengine.com/u/phischphood)\
**Post date:** [March 15, 2015, 12:19pm UTC](https://forums.unrealengine.com/t/c-collision-detection-not-triggering/311065/1 "2015-03-15T12:19:28Z")

</div>

I’m having trouble with collision detection in C++. I’ve literally spent hours trying to get it to work and still nothing (I’ve reviewed multiple tutorials and questions to no avail). I’ve tried actor and component based, hit and overlap.

The code compiles and runs, but does nothing when collisions occur.

I have the C++ side scroller template project and I’m trying to create a powerup class, which requires collision detection with the player (I’m trying collision with anything atm).

I’ve got “Simulation generates hit events” and “Generate overlap events” ticked for the sphere collision component, the sphere mesh component, the cube and the player’s capsule collider.

**PowerUp.h**

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

#pragma once

#include "GameFramework/Actor.h"
#include "Engine.h"
#include "PowerUp.generated.h"

UCLASS()
class SIDESCROLLER_API APowerUp : public AActor
{
	GENERATED_BODY()
	
public:	
	// Sets default values for this actor's properties
	APowerUp();

	// Called when the game starts or when spawned
	virtual void BeginPlay() override;
	
	// Called when this is hit
	virtual void OnPowerUpHit(AActor *SelfActor, UPrimitiveComponent *OtherActor, FVector NormalImpulse, const FHitResult& Hit);
	virtual void OnPowerUpHitActor(AActor *SelfActor, AActor *OtherActor, FVector NormalImpulse, const FHitResult& Hit);

	virtual void OnOverlapBegin(class AActor* OtherActor, class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult);

	// Called every frame
	virtual void Tick( float DeltaSeconds ) override;

	//collision primitive to use as root component
	UPROPERTY(EditAnywhere, Category = Pickup)
	USphereComponent *BaseCollisionComponent;

	//static mesh to represent the pickup in game
	UPROPERTY(EditInstanceOnly, BlueprintReadOnly, Category = Pickup)
	UStaticMeshComponent *PickupMesh;

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = Pickup)
		bool bIsActive;
	
};

```

**PowerUp.cpp**

```
#include "SideScroller.h"
#include "PowerUp.h"

// Sets default values
APowerUp::APowerUp()
{
	GEngine->AddOnScreenDebugMessage(-1, 5, FColor::Yellow, TEXT("test"));
	if (BaseCollisionComponent != nullptr){
		BaseCollisionComponent->OnComponentHit.AddDynamic(this, &APowerUp::OnPowerUpHit);
		BaseCollisionComponent->OnComponentBeginOverlap.AddDynamic(this, &APowerUp::OnOverlapBegin);
	}
	OnActorHit.AddDynamic(this, &APowerUp::OnPowerUpHitActor);
	SetActorEnableCollision(true);
 	// 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;

}

void APowerUp::OnPowerUpHitActor(AActor *SelfActor, AActor *OtherActor, FVector NormalImpulse, const FHitResult& Hit){
	GEngine->AddOnScreenDebugMessage(-1, 5, FColor::Yellow, TEXT("actor hit!"));

}

// Called when the game starts or when spawned
void APowerUp::BeginPlay()
{
	Super::BeginPlay();
	
}
	
void APowerUp::OnPowerUpHit(AActor *SelfActor, UPrimitiveComponent *OtherActor, FVector NormalImpulse, const FHitResult& Hit){
	GEngine->AddOnScreenDebugMessage(-1, 5, FColor::Yellow, TEXT("touch !"));
}

void APowerUp::OnOverlapBegin(class AActor* OtherActor, class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult){
	GEngine->AddOnScreenDebugMessage(-1, 5, FColor::Yellow, TEXT("overlap !"));
}

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

}

```

 ![34239-sidescrollernocollision.jpg](https://d3kjluh73b9h9o.cloudfront.net/original/4X/8/c/f/8cfd7f37e55121b488765efdcf061a09f74c9e53.jpeg)

---

<div class="post-metadata">

**Author:** ![CHADALAK1](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@CHADALAK1](https://forums.unrealengine.com/u/CHADALAK1)\
**Post date:** [April 3, 2015, 8:02am UTC](https://forums.unrealengine.com/t/c-collision-detection-not-triggering/311065/2 "2015-04-03T08:02:57Z")

</div>

Put UFUNCTION() above your Collision Function Prototypes

---

<div class="post-metadata">

**Author:** ![phischphood](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@phischphood](https://forums.unrealengine.com/u/phischphood)\
**Post date:** [April 7, 2015, 8:45am UTC](https://forums.unrealengine.com/t/c-collision-detection-not-triggering/311065/3 "2015-04-07T08:45:27Z")

</div>

Thanks, I’ve only just seen this, I’ll try it tomorrow.

---

<div class="post-metadata">

**Author:** ![kamrann](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@kamrann](https://forums.unrealengine.com/u/kamrann)\
**Post date:** [April 8, 2015, 8:33am UTC](https://forums.unrealengine.com/t/c-collision-detection-not-triggering/311065/4 "2015-04-08T08:33:56Z")

</div>

You haven’t created your components anywhere. Maybe you are doing it in blueprint (?), but anyway they will be null in your constructor, so your test will fail and you won’t add the delegates.

I haven’t read on documentation relating to this, but from personal experience I think it’s better to register delegates later on in an object’s initialization anyway - either PostInitComponents or BeginPlay.

---

<div class="post-metadata">

**Author:** ![phischphood](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@phischphood](https://forums.unrealengine.com/u/phischphood)\
**Post date:** [April 8, 2015, 9:37am UTC](https://forums.unrealengine.com/t/c-collision-detection-not-triggering/311065/5 "2015-04-08T09:37:37Z")

</div>

The components are created in the blueprint. I’ll try moving the delegates to PostInitComponents tonight (along with CHADALAK1’s suggestion)

---

<div class="post-metadata">

**Author:** ![anon68109364](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anon68109364](https://forums.unrealengine.com/u/anon68109364)\
**Post date:** [February 14, 2016, 7:33am UTC](https://forums.unrealengine.com/t/c-collision-detection-not-triggering/311065/6 "2016-02-14T07:33:00Z")

</div>

For me having my binding function in begin play fixed the issue

```
GetCapsuleComponent()->OnComponentBeginOverlap.AddDynamic(this, &ATheTowerCharacter::OnOverlapBegin);

```

---

<div class="post-metadata">

**Author:** ![jimmyt1988](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@jimmyt1988](https://forums.unrealengine.com/u/jimmyt1988)\
**Post date:** [March 8, 2020, 5:12pm UTC](https://forums.unrealengine.com/t/c-collision-detection-not-triggering/311065/7 "2020-03-08T17:12:17Z")

</div>

What was the solution to this?

---

<div class="post-metadata">

**Author:** ![Tarvish](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Tarvish](https://forums.unrealengine.com/u/Tarvish)\
**Post date:** [December 1, 2022, 6:45pm UTC](https://forums.unrealengine.com/t/c-collision-detection-not-triggering/311065/8 "2022-12-01T18:45:34Z")

</div>

> [@CHADALAK1](#):
>
> Put UFUNCTION() above your Collision Function Prototypes

Can confirm that putting UFUNCTION() above the callback functions solved it for me.

---

<div class="post-metadata">

**Author:** ![Mumchina](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Mumchina](https://forums.unrealengine.com/u/Mumchina)\
**Post date:** [June 28, 2023, 5:40pm UTC](https://forums.unrealengine.com/t/c-collision-detection-not-triggering/311065/9 "2023-06-28T17:40:46Z")

</div>

In my case, I’m trying to add it on a SplineMeshCompoenent.

In my header I have:

```auto
		void OnOverlapBegin(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult);

```

in the Cpp I have

```auto
SplineMeshComponent->OnComponentBeginOverlap.AddDynamic(this, &ACustomSplineSpawner::OnOverlapBegin);

```

and the declared method is :

```auto
void ACustomSplineSpawner::OnOverlapBegin(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)
{
	UE_LOG(LogTemp, Warning, TEXT("OnOverlapBegin"));
}

```

and I never see the log apreaing… ☹

Since the SplineMeshComponent is created dynamically, do I have anything special to configure for the collision to be triggered ?

---

<div class="post-metadata">

**Author:** ![3dRaven](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/3draven/32/906552_2.png) [@3dRaven](https://forums.unrealengine.com/u/3dRaven)\
**Post date:** [June 28, 2023, 6:12pm UTC](https://forums.unrealengine.com/t/c-collision-detection-not-triggering/311065/10 "2023-06-28T18:12:50Z")

</div>

You need to call

```auto
BaseCollisionComponent->SetGenerateOverlapEvents(true);

```

in your CDO
