Mesh->GetAnimInstance() returns NULL

Dear community,

I have the following problem:

I’ve created a custom UAnimInstance class called UHandAnimInstance

UCLASS(transient, Blueprintable, hideCategories = AnimInstance, BlueprintType)
class UHandAnimInstance : public UAnimInstance
{
	GENERATED_BODY()
		
	UPROPERTY(EditDefaultsOnly, Category = Phone)
    UAnimMontage* _phoneOn;

	UPROPERTY(EditDefaultsOnly, Category = Phone)
	UAnimMontage* _phoneOff;
public:
	void ShowPhoneLight(bool doShow);
};

I’ve reparented my animation blueprint called AnimHandBP and set up both montages. So far it works perfectly.

I have my FirstPersonCharacter blueprint derived from my C++ class called ATheForestCharacter
This character has a Mesh called HandAnim and there I’ve set up in the editor:

Animation Mode: Use Animation Blueprint

Anim Blueprint Generated Class: AnimHandBP_C

Please note that the mesh itself is created in C++ in a constructor of ATheForestCharacter like this:

HandAnim = CreateDefaultSubobject<USkeletalMeshComponent>(TEXT("HandAnim"));
HandAnim->SetOnlyOwnerSee(true);		
HandAnim->AttachParent = FirstPersonCameraComponent;
HandAnim->RelativeLocation = FVector(29.801f, 17.787f, -28.2f);
HandAnim->RelativeRotation = FRotator(0.f, 0.f, -90.f);
HandAnim->RelativeScale3D = FVector(0.7f, 0.7f, 0.7f);

Compiled / Saved etc.

Now back in my ATheForestCharacter I am trying to call method ShowPhoneLight() of the animation blueprint and here lies the problem:

void ATheForestCharacter::BeginPlay()
{
	Super::BeginPlay();
	if (HandAnim)
	{
		UHandAnimInstance* myHand = Cast<UHandAnimInstance>(HandAnim->GetAnimInstance());
	}
}

This code always returns NULL into myHand.

Please note that even:

UAnimInstance* myHand = HandAnim->GetAnimInstance();

returns also NULL.

I’ve read many tutorials and threads and it sounds like the way to go so I believe I am missing something terrible obvious.

Any advice would be greatly appreciated.

Thank you!

Nevermind - found a solution. You must have assigned skeletal mesh or this doesn’t work.

1 Like

Could you please elaborate more on your solution? I’m having the same problem, animation is always null, even though i have assigned a valid skeletal mesh in the editor.

Thanks!

In my case “HandAnim” didn’t have skeletal mesh assigned. Only the animation blueprint.

You did this in the editor or directly in code? Thanks! :slight_smile:

Nevermind - just found out my problem: instead of setting “Anim Blueprint Generated Class” to my blueprint i was setting it directly to my custom AnimInstance parent class. I just read “class” in the field and didn’t give it a second thought.

Thanks for your help!

For me, GetAnimInstance() alwayrs returned nullptr during the actor’s contstructor/SetupAttachment.

GetAnimInstance() returned my class during BeginPlay().