Editor crashes if i use ->ActorHasTag()

Hi there,

i have a strange Problem. I learning UE C++ and wanted to try something. I have set up a line trace. With this line trace i get the actor that was hit. I can log out the Name of the actor and all works fine. But if i write this…

if (ActorHit->ActorHasTag(FName(TEXT("Test"))))
//...then do someting

then the Editor crashes. It only crashes when i am trying to find out if the actor has a tag. What i am doing wrong? I checked with nullptr, with valid and so on. I don’t Need this now, but i wanted to know whats going wrong.

If your valid checks are all correct then that code should be fine. When we use the ActorHasTag, I don’t use the FName() cast. Try removing that. Is it definately that line that is the issue?

I used all variations from: ActorHasTag(TEXT("") to ActorHasTag() to ActorHasTag("")…
If i cut this part out and only check: if (ActorHit){}, then all work fine. Hm. Today i have a look at it. Maybe i missing something.

Do you have the rest of the code around where the crash is? It may not be related to the ActorHasTag call.

 if (ActorHit && ActorHit->ActorHasTag(FName(TEXT("Test")))){  
//...then do someting  

Try this. If ActorHit is null, trying to run code from it will crash. The “ActorHit &&” before ensures that ActorHit exists first, before trying to see it’s tag.

This is how i do it


Strange. I have tried this too. But i have a look at it. Thanks for your help.

ActorHit is obviously null (i.e. you didn’t hit an actor) and you cannot dereference a null pointer. The answer from @Juice-Tin is correct.

The Actor isn’t null. Like i wrote. If i only check if an actor was hit, then it Returns true and all work fine.

If the trace hits something and returns true, that doesn’t necessarily mean that the actor is valid, because not all components have an owner actor (like BSP geometry). So before you do anything with your actor, simply check if HitResult.GetActor() != nullptr.

Oh ok. I will try that.

You should be checking whether the FHitResult even hit something in the first place:…Hit/index.html. If bBlockingHit is false then don’t attempt to call any functions on the GetActor() result as it will cause a memory violation crash (aka trying to call a method on uninitialised memory).

Edit: didn’t read previous replies.