Find Blurprint actor with c++

Hi everyone,

So I’m stuck with this issue of not being able to find a BP actor in my C++ code.
I used LineTraceSingleByObjectType then I put the result in a FHitResult called hit.
Basically I want to compare my blueprint actor with what is in hit.GetActor().
Any advices will be appreciated.
PS : My other code works fine if I remove the if statement.

The code below crashes when it arrives at the if statement.

Thank you

AActor* actorHit = hit.GetActor();
If (actorHit->GetName() == "myActor_BP") //
 //Do something useful here

Classic case of a nullptr exception.

Your actorHit is not guaranteed to have a valid value, meaning, it might not point to an actor. After all, your LineTrace might not find a compatible target to hit.

Since you are trying to call a function on an object that in some cases might be nullptr, the engine crashes.
Put an “if(!actorHit) return;” or some kind of similar structure before your if, checking if your actorHit field is valid first.

if(actorHit && actorHit->GetName() == “myActor_BP”)

will do the job, if first condition fails 2nd is not even checked and won’t crash

Hi, that fixed it !

Thank you.