Blueprint Class Load Error

In my main zombie character (made in C++) I want to attach the Animation Blueprint to the Mesh. I have done it successfully with the following code:



       UClass* AnimInstanceClass = FindObject<UClass>(ClassPackage, TEXT("/Game/Meshes/SkeletalMeshes/Zombie/zombie_AnimBlueprint.Zombie_AnimBlueprint_C"));
	
	if (AnimInstanceClass) {

		MeshComponent->SetAnimInstanceClass(AnimInstanceClass);
	
	}


The code works perfectly, but when I restart the editor, the animation blueprint is not attached. I have to open the Animation Blueprint, and then, without changing nothing, it works. If I build the game without opening the blueprint the same happens. This is not just for Animation Blueprints, but for all blueprint classes. Thanks in advance.

FindObject won’t work if the class isn’t loaded already, and IIRC, it doesn’t work in packaged games at all since you’re using a string reference. You can call LoadObject to load an object, though.

You’re much better off creating a UPROPERTY(EditDefaultsOnly) which points to the animation, and using that in code. TAssetPtr or a regular pointer is probably what you want.