Get a reference to a UWidgetComponent in C++

So here is the situation. I have a Character, that has a Widget Component attached to it, with a textbox.

From ANOTHER character, i do a raycast each frame to check if i can see the other character, and when i do, i want to SetVisibility(true) on the WidgetComponent of the OTHER character.

Thanks to the raycast i have a reference to the other Actor, but Casting always fails, and im using something like this:

UWidgetComponent target = Cast<UWidgetComponent>(Hit.GetActor()>GetComponentByClass(UWidgetComponent::StaticClass));

target->SetVisibility(true, false);

What am i doing wrong here? The aforementioned example crashes UE4.

Hi alexformosoc,

You haven’t Null checked the hit actor or the target widget component pointer.

Cheers,