OnComponentBeginOverlap not working

What I’m trying to create is an obstacle that the player will pass through, but get damaged when he does (think of crashing head first into some glass). I have added a new collision profile for it in DefaultEngine.ini like this:

[/Script/Engine.CollisionProfile]
+Profiles=(Name="ObstacleMesh",CollisionEnabled=QueryAndPhysics,ObjectTypeName="WorldDynamic",CustomResponses=((Channel="Pawn",Response=ECR_Overlap),(Channel="Vehicle",Response=ECR_Overlap),(Channel="Camera",Response=ECR_Ignore)),HelpMessage="WorldDynamic object that overlaps Pawn, Camera, and Vehicle. All other channels will be set to default. ",bCanModify=False)

My obstacle class is set up like this:

UCLASS()
class AObstacle : public AActor
{
	GENERATED_UCLASS_BODY()

	UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Mesh)
		TSubobjectPtr<UStaticMeshComponent> OriginalMesh;

	UFUNCTION()
		void OnOverlap(class AActor* OtherActor, class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex);
};

And the definitions are:

AObstacle ::AObstacle(const class FPostConstructInitializeProperties& PCIP)
: Super(PCIP)
{
	OriginalMesh = PCIP.CreateDefaultSubobject<UStaticMeshComponent>(this, "OriginalMesh");
	OriginalMesh->BodyInstance.SetInstanceSimulatePhysics(false);
	OriginalMesh->SetCollisionProfileName(TEXT("ObstacleMesh"));
	OriginalMesh->OnComponentBeginOverlap.AddDynamic(this, &AObstacle::OnOverlap);
	RootComponent = OriginalMesh;
}
    
void AObstacle::OnOverlap(class AActor* OtherActor, class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex)
{
	if (GEngine)
		GEngine->AddOnScreenDebugMessage(-1, 5, FColor::Red, "Overlapped in C++");
}

But the event won’t fire when the pawn enters the mesh. I tried adding the overlap event in the Blueprint subclass I’m placing in the scene, and it fires there, which means the problem is probably not with the setup of collision profiles and channels, but not in C++. Any ideas why this is happening?

Answering my own question here, but anyway:

The problem was that I had created the blueprint BEFORE adding the code for the overlap part, and for some reason it wasn’t working anymore. So I deleted the blueprint and re-created it, and now everything is working fine. Leaves me wondering though, why should this happen at all?

Thanks for update. I created my BP after C++ class but made a change around my SphereComponent and while it showed up in BP editor it wasn’t until I recreated blue print that it worked so you saved me. Then wasted an hour trying to figure out a crash until realized my Hud class got reset on GameMode bp. But hey I’m having a ball learning to develop with UE :slight_smile:

I wish I had found your answer few hours earlier

great, i found this answer after 5 hours, but i think there must be a way to synhcronize the code?

still happening on Sept 10, 2016 :o(

This kind of thing really makes me appreciate Unity’s decision to base their engine on the .net framework. You just can’t beat C#'s simplicity, ease of use and all the existing open source code.

still happening on March 2017!

Two years now and i spend also an hour finding the problem.
I’m just starting with UE but those issues I have, make me overthink my decision to choose UE!

I have just 3 BPs derived from my c++ class, so I “only” have to re-implement those. But I really wonder what happens when I have about 20-30 derived from such a class, that’s a nightmare.

Since umitcel’s question is still unanswered now it doesn’t seem there is a way of synchronizing i guess??

2018 still happening.

But I have seen that moving the OriginalMesh->OnComponentBeginOverlap.AddDynamic line from the constructor to BeginPlay seemed to solve the issue. Not sure what the other implications of this are.

I still don’t really know what to put in the constructor and what to put in BeginPlay. There are also cases that should work but don’t. For example, last I checked, you couldn’t assign a physics asset in the constructor as it would crash the engine at startup.

As @anonymous_user_68c13e9a pointed out on the selected answer’s comment, do the event binding on BeginPlay instead of the constructor.

In the constructor only do variable initialisation, and subobject creations. The constructor is executed even in edit mode which is why you see the components you attach to your RootComponent while in the editor. This is similar to how Construction Scripts work on blueprints.

In begin play do things like event bindings, bones attachment, or things that you want done while play time.