Casting fails - trying to cast to USceneComponent

Good day,

I’ve a scene component (Door handler) that I can add to any door and makes it openable by pressing a Key.

In Mycharacter.cpp, I try to cast to my uscenecomponent, but it fails regardless whatever I do and I don’t see a clear reason why, every other casting throughout my codes work fine.

void AWLL_CharacterChild::OnOverlapBegin(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult &SweepResult)
{
   if ( (OtherActor != nullptr ) && (OtherActor != this) && ( OtherComp != nullptr ) )  
   {
       UDoorHandler* TempDoorHandler = Cast<UDoorHandler>(OtherActor->GetRootComponent());
       if(TempDoorHandler)
       {
           
           if(OtherActor == TempDoorHandler->GetOwner())
           {
               DoorHandlerFunction = TempDoorHandler;
               
           }
           
       }
       else UE_LOG(LogTemp, Warning, TEXT("CASTING TO DOOR HANDLER FAILED - CharacterChild [OverLap Function]")); 

   }

}

I tried to replace this with the above but with no luck.

UDoorHandler* TempDoorHandler = Cast<UDoorHandler>(OtherActor);

I’ve been trying to do every possible option I know of but it always fails to cast,
Any suggestions?

I don’t quite understand the setup.
Is DoorHandler derived from UChildActorComponent so that you can add it to any other actor?
And it contains a trigger box in?
Is that trigger box the root component of DoorHandler?
Please elaborate.

If it’s a component added to the door, then OtherActor in the overlap function will return the door actor, and its root component is not DoorHandler. Maybe try Cast<UDoorHandler>(OtherComp) instead?

UDoorHandler* TempDoorHandler = Cast(OtherActor->GetRootComponent());

Your UDoorHandler component is most likely not the root component.
Try this instead :

UDoorHandler* TempDoorHandler = OtherActor->FindComponentByClass<UDoorHandler>();
1 Like

You guessed it right,

but I tried the suggestion you gave and it it didnt quite work.

UDoorHandler* TempDoorHandler = OtherActor->FindComponentByClass<UDoorHandler>();

is what I was looking for,

Much thanks to Chatouille.