In Unity, is assigning an Animator Controller to more than one script not allowed, even if those scripts are attached to the same GameObject?

Hi every one i am not able to
drag and drop the animator controller called SSlime into public animator field of
playerAttack.cs at the inspector whereas i am able to drag drop it in the public animator field of
slime_script.cs at the inspector. The slime_script.cs and playerAttack.cs are both assigned to a single
gameobject, slime.
the playerAttack.cs is responsible for attacking animation of the gameobject slime
whereas the slime_script.cs is responsible for idle animation and other health of the gameobject slime
The SSlime animator controller contains animation transition of both attack and idle and other animation

the animator field in the slime_script.cs was declared as shown below

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class slime_script : MonoBehaviour
{
    public float speed; 
    public Animator animator;
....
...
.
}

and that of the animator filed in the playeAttack.cs was declared (as shown below) almost same as above

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class playerAttack : MonoBehaviour
{

    private GameObject attackArea = default; //this is the area where the attack happens
    public Animator animator ;
...
..
..

}

so my question is in Unity, is assigning an Animator Controller to more than one script not allowed, even if those scripts are attached to the same GameObject?

There is no limit on the number of components that your Animator can be referenced from.

ok thanks for clearingfying that … and in my case i am not able to drag and drop the animator contorller into the animator field on one of two script of a gameobject… have you encounter this problem before ?? if so how can i solve it , just a little hint would do fine

thanks

Could you show a screenshot of what your GameObject looks like in the Inspector window, with the Animator component and two script components?

1 Like

yeah sure ,
i have attached the screenshot

In the screen shot the animator field of the PlayerAttack.cs is not accepting the Slime animator controller when drag over it

You want your script components to reference the Animator component on the GameObject, not the AnimatorController asset in your project folder. Try one of these:

  • Drag and drop the ‘slime’ GameObject from the hierarchy onto the Animator field on your script
  • Drag and drop the ‘Animator’ component from further up in the Inspector onto your script
  • Hit the object selector popup button at the right hand side of your field in the Inspector and choose ‘slime’ from the list.

You could also solve this in code by calling GetComponent<Animator>() in the Awake() method for your scripts.

1 Like

Yeah brother thank you so much
i have to assigned the Animator to the animator field not the animator controller.
i have Drag and drop the ‘Animator’ component from further up in the inspector as you have suggested and its working it now make sense …thank you so much i have also attached picture .
thanks you so much brother appreciate it

1 Like