Animator component can't be found by the Player gameobject (nested prefab)

When I execute the code I get the following exception:

MissingComponentException: There is no ‘Animator’ attached to the “Player” game object, but a script is trying to access it.

In the previous lesson we created a nested prefab Player > Character_Knight_Soldier. I realized that the nested prefab Character_Knight is the one with the animator component, while the Mover.cs script is in the father Player. So I was wondering, did you reassign the Animator from the son object to the father Player?
However, I wonder: couldn’t the script access the component of its children game objects? What’s the purpose of having some components in the child object if it can’t?
Thanks!

1 Like

In the course, we create an Animator in the top level Player component and remove the one on the Character_Knight_Soldier.
You could also, instead of doing that, call GetComponentInChildren<Animator>() instead of GetComponent<Animator>() to get hold of the one on the Character_Knight_Soldier GameObject.

1 Like

I must have missed that part. Thanks!

1 Like

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

Privacy & Terms