UBoxComponenet C++ OnOverlapEvent not firing. I don't understand what I am doing wrong

Hello,

I’ve been struggling to get the overlap event to work from my C++ code. Can someone help me understand what is the issue? My code compiles without an error but fails to work when the overlap happens in the game.

Header File Relevant code

class UBoxComponent;

	// A box volume trigger which on overlap will destroy self
	UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = "Components", meta = (AllowPrivateAccess = "true"))
	UBoxComponent* EndTrigger;

	// Function to perform operations when character overlaps the EndTrigger
	void OnEndTriggerBeginOverlap(UPrimitiveComponent* OverlappedComp, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult);

In the CPP File

#include "Components/BoxComponent.h"

	// Initialize the box component - EndTrigger
	EndTrigger = CreateDefaultSubobject<UBoxComponent>(TEXT("EndTrigger"));
	EndTrigger->SetRelativeLocation(FVector(2000.0f, 0.0f, 0.0f));
	EndTrigger->SetBoxExtent(FVector(0.0f, 2000.0f, 2000.0f));
	EndTrigger->SetCollisionResponseToAllChannels(ECollisionResponse::ECR_Ignore);
	EndTrigger->SetCollisionResponseToChannel(ECollisionChannel::ECC_Pawn, ECollisionResponse::ECR_Overlap);
	EndTrigger->SetGenerateOverlapEvents(true);
	EndTrigger->SetupAttachment(SceneRoot);

void AMyActor::BeginPlay()
{
	Super::BeginPlay();
	EndTrigger->OnComponentBeginOverlap.AddDynamic(this, &AMyActor::OnEndTriggerBeginOverlap);
	
}

void AMyActor::OnEndTriggerBeginOverlap(UPrimitiveComponent* OverlappedComp, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)
{
	PrintStringToLog(TEXT("EndTriggerOverlap called"));
	Destroy();

}

The above however log/function however never gets fired. Can someone tell me what am I doing wrong?

Okay, I figured it out by looking at a sample project reference. I forgot to add the UFUNCTION() Macro

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.