Why Can't I bind a function to this component's OnComponentHit?

Try AddRaw instead of AddDynamic

I have a class derived from a character. I’m trying to add some functionality when a hit event occurs.
In my .h I have added (under public):

UFUNCTION()
void OnHit(AActor* other, class UPrimitiveComponent* otherComp, FVector normalImpulse, const FHitResult& hit);

And in the constructor of my .cpp I have added:

GetCapsuleComponent()->OnComponentHit.AddDynamic(this, &AMainCharacter::OnHit);

Lastly, I have defined the function in my .cpp

void AMainCharacter::OnHit(AActor* other, class UPrimitiveComponent* otherComp, FVector normalImpulse, const FHitResult& hit)
{
	if (GEngine)
		GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, "Hit.");
}

I’ve researched numerous topics, and, as far as I can tell this is the correct code. However, I do not get a printed statement when a hit occurs. The collider in the blueprint is definitely set to generate hit events, yet still I get nothing. If I implement the functionality in Blueprint, it works. So, I don’t think it’s a problem with collision channels or anything else. I’ve also tried adding another collision component to test if I could get that one to work. I could not. As a matter of fact, I had no access to the “Simulation Generates Hits” bool within Blueprint with that collider, so, I couldn’t even get that to generate hits in Blueprint! Any ideas about what might be going wrong?

I got error C2039: ‘AddRaw’ : is not a member of ‘FComponentHitSignature’
Do I need to change some parameters?

No not really, but he might have a problem with the key word class, that might also be the reason its not working with ‘AddDynamic’

Btw the constructor might be to early to bind this function, a better place would be OnConstruction

I couldn’t really explain why the word class is in the function prototype, but, that’s how the examples in some of Epic’s tutorials and other answer hub questions have it. However, it was not in the actual class definition in those examples, so, I removed it from there. But, the function is still not being called when the capsule collides.

I deleted the blueprint and made another. It started working.