Overlap function does not work

Hi, I am making a function that fires when the character overlaps, but it doesn’t work (at least it doesn’t output “OPA”)
The compiler does not print errors
Header File



#pragma once

#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "Engine.h"
#include "ACollectible.generated.h"

UCLASS()
class TESTPROJECT_API AACollectible : public AActor
{
    GENERATED_BODY()

public:    
    // Sets default values for this actor's properties
    AACollectible();

    UPROPERTY(VIsibleAnywhere)
        UStaticMeshComponent* SM;
    UPROPERTY(VIsibleAnywhere)
        URotatingMovementComponent* RotatingComp;
    UFUNCTION()
        void OnOverlap(AActor* MyOverlappedActor, AActor* OtherActor);

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

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

};


Source File


#include "ACollectible.h"
#include "TestProjectCharacter.h"

// Sets default values
AACollectible::AACollectible()
{
     // 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;

    SM = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("StaticMesh"));
    RotatingComp = CreateDefaultSubobject<URotatingMovementComponent>(TEXT("RotatingMovementComponent"));
    OnActorBeginOverlap.AddDynamic(this, &AACollectible::OnOverlap);
}

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

}

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

}

void AACollectible::OnOverlap(AActor* MyOverlappedActor, AActor* OtherActor)
{
    GLog->Log(TEXT("OPAOPAOPAOPA"));
    ATestProjectCharacter* Char = Cast<ATestProjectCharacter>(OtherActor);
    if (Char)
    {
        FText Text = FText::FromString(GetName());
        GLog->Log(TEXT("ItemAdded"));
        Char->AddItemToUI(Text);
        Destroy();
    }
}


What are the collisions settings of your static mesh? If you set “OverlapAllDynamic” or “OverlapOnlyPawn” it will work.

I set “OverlapAllDynamic” and “OverlapOnlyPawn” and it did not bring any result. :frowning:

I must add that this is an actor, not a trigger box. Does it matter?

Your code works perfectly to me:

Strange, I created a new class and it also worked, thanks

There’s the possibility that the OnActorBeginOverlap has been serialized to a save file while being empty, and that it is therefore being overwritten back to empty every time you load your actor.
As a general rule, if you try to modify an inherited object attribute that isn’t marked as transient, you should do so in one of the PostSomething functions (PostInitializeComponents, PostLoad etc…) as opposed to the constructor.

Otherwise you’ll get strange behavior when trying to make an instance of something work, if that instance has already been saved in your map once.