How to add OnClicked event for StaticMeshComponent?

Hej,
I’ve been trying to click on StaticMeshComponent during Run-time, but not able to click on particular StaticMeshComponent. I’ve tried the below logic to click and pop-up message dailog, but triggered a breakpoint at
DelegateSignatureImpl_Variadics.inl

ensureMsgf(this->IsBound(),
TEXT(“Unable to bind delegate to ‘%s’
(function might not be marked as a
UFUNCTION)”), *InMacroFunctionName);

In .cpp

    StaticMeshComponent = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("StaticMeshComponentCOMP"));
        	ConstructorHelpers::FObjectFinder<UStaticMesh> StaticMesh_obj(TEXT("/Game/StarterContent/Meshs/Chairs_Chair1"));
        	StaticMeshComponent->SetStaticMesh(StaticMesh_obj.Object);
        	StaticMeshComponent->OnClicked.AddDynamic(this, &AMyActor::OnClick);
    	StaticMeshComponent->AttachTo(RootComponent);

void AMyActor::OnClick(UPrimitiveComponent* pComponent)
{
	FMessageDialog::Open(EAppMsgType::Ok, FText::FromString(TEXT("Clicked")));
}

In .h

void OnClick(UPrimitiveComponent* pComponent);

Could you guys please help me in this issues Or guide me another efficient logic to solve my problem.

P.S: Game Mode : Mouse Click mode

The error message is telling you exactly what to do. Just add an ampersand before your method reference to make a method pointer.

Instead of:

StaticMeshComponent->OnClicked.AddDynamic(this, AMyActor::OnClick);

Use:

StaticMeshComponent->OnClicked.AddDynamic(this, &AMyActor::OnClick);

In your .h file, add UFUNCTION(Category=Default) above the OnClick() declaration.

Thanks for point out the mistake, But I have issues with Delegate for long time.

thanks a lot. This solved my problem.

Need a help to set the material inside OnClick() Function. I tried with below logic, but the editor crashed. How can I approach??

   ConstructorHelpers::FObjectFinder<UMaterial> MeshMaterial(TEXT("/Game/StarterContent/Materials/M_Glass"));
    
    	// Set properties for Staic mesh component
    	
    	StaticMeshComponent->SetMaterial(0, MeshMaterial.Object);
    	StaticMeshComponent->AttachTo(RootComponent);
  • FObjectFinder can only be used in constructors. If you use it in a regular method, it’ll fail.
  • The reference string may not be correct. I would expect it to be something like "/Game/StarterContent/Materials/M_Glass.M_Glass".

If neither of those is the issue, you should post the exact error message and stack trace you get in the crash as a separate question.