UnPossessed called BEFORE OnPlay => crashes UE

The Gun pointer is null on the first call to UnPossessed.

Indeed, I was experiencing the same problem.

Problem is fixed after putting the code to check for null pointer:

    if (Gun != nullptr)
	{
		Gun->AttachToComponent(GetMesh(), FAttachmentTransformRules(EAttachmentRule::SnapToTarget, true), TEXT("GripPoint_0"));
	}
8 Likes

i can vouch for this answer, worked for me!

The solution brought by @Pavlex4 is correct, but I have another question:

When my UE 4.16 started to crash with this code, I tried to check the condition with ensure. But it kept crashing. If ensure is just like higher state of checking, why it kept crashing, and the normal if like suggested by @Pavlex4 just works?

Did you type the ensure with “!” in front?

@Edgar_Hernandez Thank you for response. Of course I checked it with “!=”. The idea I thought was: I want to be sure that Gun doesn’t point to a null pointer. Checked it with both nullptr and NULL. But well, if a simple IF works - it’s fine :slight_smile:. It just wasted me some time, because I thought that if ensure doesn’t work - the problem is somewhere else. Anyways a nice lesson for future.

Bro! In the Q&A of this lecture someone pasted a block of code where the “ensure” is used as you want. Check there to see any difference with your format. The question is titled “Crash on Play” by Chuanke.

@Edgar_Hernandez Thanks again for reply :slight_smile:. Well, I checked that now, and from what I remember, I had it slightly different. So probably I had a mistake. Nevertheless now it’s just for education purpose cause I used the solution from this topic and works perfectly!

The solution given by @Pavlex4 does work on Unreal Engine 4.21 as of 2019-05-08.

The engine does not crash again.

Privacy & Terms