The animator component works fine with a regular hierarchy object, but won't work with its prefab

So my code for my game’s enemy’s animator component is

private Animator enemyAnim;

before the Start method, and in the Start method it says

enemyAnim = GetComponent<Animator>();

which has been working perfectly when I had just one enemy in the hierarchy, but when I have been using its prefab Unity says:

“UnassignedReferenceException: The variable enemyAnim of EnemyBehavior has not been assigned.
You probably need to assign the enemyAnim variable of the EnemyBehavior script in the inspector.”

How can I get it to work with the prefab as well?

If it helps, everything works except when I try to attack the enemy, in which case the animation is triggered by the player. Everything the enemy does on its own works fine.

Ok nvm i fixed it