OnBeginCursorOver not firing when Mouse Cursor Overlaps UStaticMeshComponent

I am using Unreal Engine 5.1 and I’ve created a UActorComponent (Called UClickableComponent) that Attaches to a AStaticMeshActor (i’m using a cube for testing) and fires the OnBeginCursorOver event when the Mouse Cursor Hovers over the Mesh. I have ShowMouseCursor, Enable Click Events and Enable Mouse Over Events all set to true in PlayerController class. This Player Controller is set as the active player controller.

My Code:

#pragma once

#include "CoreMinimal.h"
#include "Components/ActorComponent.h"
#include <Engine/StaticMeshActor.h>
#include "ClickableComponent.generated.h"


UCLASS( ClassGroup=(Custom), meta=(BlueprintSpawnableComponent) )
class TESTMOTHAFUCKA_API UClickableComponent : public UActorComponent
{
	GENERATED_BODY()

public:	
	// Sets default values for this component's properties
	UClickableComponent();
	UPROPERTY(EditAnywhere)
		UStaticMeshComponent* AttachedMesh;

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

public:	
	// Called every frame
	virtual void TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction) override;
	
	UFUNCTION()
		void CursorOverlap(UPrimitiveComponent* Overlapped);

};
// Called when the game starts
void UClickableComponent::BeginPlay()
{
	Super::BeginPlay();

	AActor* Owner = GetOwner();
	if (Owner)
	{
		UStaticMeshComponent* StaticMesh = Cast<UStaticMeshComponent>(GetOwner()->FindComponentByClass(UStaticMeshComponent::StaticClass()));
		if (StaticMesh)
		{
			StaticMesh->OnBeginCursorOver.AddDynamic(this, &UClickableComponent::CursorOverlap);
			GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Yellow, FString::Printf(TEXT("StaticMesh has been binded to CursorOverlap")));
		}
	}

	RegisterComponent();
}

void UClickableComponent::CursorOverlap(UPrimitiveComponent* Overlapped)
{
	GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Yellow, FString::Printf(TEXT("Overlapped with Cursor and Function Called")));
};

This is my Code for the UActorComponent (UClickableComponent) and when this is attached to an AStaticMeshActor, nothing happens. Im not sure why either and I cant find what im doing wrong.

This Blueprint code works fine and i’ve been trying to mimic it, with no success. If I attach this blueprint to the AStaticMeshActor, It fires the OnBeginCursorOver Event and everything works as expected, this is not the case for my C++ implementation.

This is the Hierarchy of the Cube, which has a StaticMeshComponent and my ClickableComponent(C++ implementation)

All in All, I want my UClickableComponent::CursorOverlap function to fire, when the mouse cursor hovers over the StaticMeshActor that my UClickableComponent is attached to. As stated previously, the blueprint I attached works fine.

Any Help or guidance would be apprecaited.

1 Like