Casting AActor* to PlayerClass* - Engine crash

First off hello, this is my first post here, hope I make it understandable enough.

I tried to find someone with a similar problem to mine (not just on here) but I sadly didn’t find anything.

So I’m making my own post.

The problem is that the engine crashes every time my code gets to the line that has the Cast method on it.

void ASpell::OnPlayerEnterHealArea(UPrimitiveComponent * OverlappedComponent, AActor * OtherActor, UPrimitiveComponent * OtherComponent, int32 BodyIndex, bool bFromSweep, const FHitResult & SweepResults)
{
	if (!OtherActor->IsA(AProjectNameCharacter::StaticClass())) return;

	AProjectNameCharacter* NewCharacter = Cast<AProjectNameCharacter>(OtherActor);
}

Sadly the crash report doesn’t give me any information.

This also happened on 4.16, so I can safely assume that it’s me that is doing something wrong, since I just recently started working with UE4.

If you need more information I’d be happy to answer any questions.

Thanks in advance!

Instead of answering directly I’m gonna show you a very useful tool to help you find the problems generated by this kind of issues, in VS you can go into Debug->AttachToProcess and from there just attach the UE4Editor.exe (your editor instance for that game). Now when you run the game and this issue occurs you will have a nice break in your code showing you the state of the variables and all the info you may need.

Now for the answer to your question, I assume that the cast fails (since you seem pretty sure about the game breaking when it reaches your line of code), try using an auto NewCharacter = Cast(OtherActor); instead to see if saving the type in question is the issue here or the cast itself.

If you already knew about attaching to process from VS then I apologize and hope that someone else finds this information useful in the future :slight_smile:

For some reason I cannot spell the cast the right way:

auto NewCharacter = Cast<AProjectNameCharacter>(OtherActor);

I just tried that and it still crashes.

When I try casting from UClass* (OtherActor->GetClass()) the cast is just unsuccessful but the engine doesn’t crash.

I think IsA() does that automatically but I did it anyway and it’s still crashing.

Have you verified OtherActor isn’t null?

Change this line:

if (!OtherActor->IsA(AProjectNameCharacter::StaticClass())) return;

to

if (!OtherActor || !OtherActor->IsA(AProjectNameCharacter::StaticClass())) return;

Hey I found the solution for my problem.

I should really read up more on the Software I’m working with, maybe then I would’ve known that Unreal has built in Garbage collection.

So my problem was that I attempted to delete a bunch of things I shouldn’t delete (apparently) because it’ll make the Editor crash.

Don’t know what to say, I only have myself to blame for this.

Learn from my mistakes. lmao

Anyway, thanks to anyone that tried to help me, I really appreciate you guys.