Actor OnClicked is not triggered C++

I Have an actor like this

UFUNCTION()
void AMyClass::OnClick(AActor* TouchedActor, FKey ButtonPressed){}

and the constructor has

MeshComponent = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("MeshComponent"));
	MyBoxComponent= CreateDefaultSubobject<UBoxComponent>(TEXT("MyBoxComponent"));
	SetRootComponent(MeshComponent);
	MyBoxComponent->SetupAttachment(MeshComponent);

and In BeginPlay() I have this

	OnClicked.AddDynamic(this, &AMyClass::OnClick);

Now I cannot trigger the OnClick event

my PlayerController has enable Click Events

bShowMouseCursor = true;
bEnableClickEvents = true;
bEnableMouseOverEvents = true;

Note all this mess happened after I renamed the class, Also I have to spam click the actor(like 20 times) to get a single trigger

  1. Make sure your OnClick() is marked as UFUNCTION();
  2. Try binding on BeginPlay(), not in the constructor.
1 Like

Thanks and I apologize for not mention this earlier, but I already have the UFUNCTION macro on the OnClick and the binding happens begin play too.
I updated my question to reflect this

Check which actor/component is returned when you click. Might be the issue with collision channels.
You can use PlayerController->GetHitResultUnderCursor(…) for that.

I have left click input binding event in the editor
in this function I can see that when I click all channels returns myCamera except for the The Visibility channel, it returns the actor Indeed but my onclick event is not triggered. sometimes they all return my actor though

I’ve frankly never used OnClick events for actors, so IDK what the issue might be.
But if GetHitResultUnderCursor() always returns the right actor, why not use it to call the function you need instead OnClick?

There’s no problem with using Hit.
My first attempt was using a line tracing, then I moved to OnClick when it worked because it separated the code and made it cleaner. this is the only reason I am interested in it. I guess I will use it, until I find a way to fix it

Wanted to give my insight and what have worked for me:

Enable Click Events on player controller
image

And set Input Game Mode to “Game and UI”
image