Nullptr when trying to cast UAnimInstance to custom object

I’m following a tutorial from a book, I just finished setting up animations for the player character, but I have problems with the code from the book; It wont work!

It says in the book

void APlayerAvatar::Tick(float DeltaTime)
{
	Super::Tick(DeltaTime);

	UPlayerAvatarAnimInstance* animInst = Cast<UPlayerAvatarAnimInstance>(GetMesh()->GetAnimInstance());
	animInst->Speed = GetCharacterMovement()->Velocity.Size2D();

}

It compiles, and builds with no errors. But when I enter playtest mode in the editor it crashes whereas an ‘unreal crash’ window comes up. The debug tells me that there is a write access violation because animInst was nullptr.

I tried:

auto animInst = Cast<UPlayerAvatarAnimInstance>(GetMesh()->GetAnimInstance());
	animInst->Speed = GetCharacterMovement()->Velocity.Size2D();

and also this:

UAnimInstance* temp = GetMesh()->GetAnimInstance();
UPlayerAvatarAnimInstance* animInst = Cast<UPlayerAvatarAnimInstance>(temp);
animInst->Speed = GetCharacterMovement()->Velocity.Size2D();

But same thing happens.
I understand why I cant do anything with a nullptr, but unsure why it happens.
I’ll try to solve this, but any kind of help is appreciated.

Version of the engine is 5.2.1

Thanks!

1 Like

I’ve solved it myself. It turned out I missed out on one important step which was to set my playeravatar blueprint to use the animation blueprint.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.