Set AnimInstanceClass at the end of AnimMontage

Hello, I’m trying to change the anim class when anim montage has finished, but I’m getting assertion failed for !bPostEvaluatingAnimation and PostAnimEvaluation already in progress, recursion detected for SkeletalMeshComponent .

if (animInstance && MontageToPlay) animInstance->Montage_Play(MontageToPlay, 1.f);
FOnMontageEnded onMontageEnded;
onMontageEnded.BindLambda([this](UAnimMontage* montage, bool bInterrupted) { GetMesh()->SetAnimInstanceClass(NewAnimClass); });
animInstance->Montage_SetEndDelegate(onMontageEnded);

Am I missing something before

GetMesh()->SetAnimInstanceClass(NewAnimClass);

?