Animator Controller created by script is not recognized by Animator

Hey everybody, I seem to be having trouble creating an Animator Controller through script that the Animator will actually recognize.

A bit of context: I have a custom editor that allows me to add new entries to a list of NPCs that spawn in the game. In order to save a bit of time, I’m also having it create empty animations and an animator controller assets to use with the new characters. I do this with the following code:

AssetDatabase.CreateAsset(new AnimatorController(),"Assets/Sprites/Unit Sprites/Animations and Controllers/New Unit Animation Controller.controller");

While this does indeed create a new controller, when attempting to open it in the Animator I’m greeted with “Cannot show controller from asset bundle”.

I use a near identical script to create new animation assets without any problems, so I’m really not sure what I’m missing here?

I just had the problem and found out that it was only because the controller created by script does not have any layer.

You can fix it either by adding manually a new layer after you’ve created your controller, or adding this code :

 AnimatorController controller = new AnimatorController();
 controller.AddLayer("Base Layer");
 AssetDatabase.CreateAsset(controller, pathNewAnimator + ".controller");

I found the answer. It was much simpler than I was making it out to be. The datatype is not assumed when the function is called. You have to add “.controller” to the end of the path.

For example:

UnityEditor.Animations.AnimatorController controller = UnityEditor.Animations.AnimatorController.CreateAnimatorControllerAtPath(“Assets/AC”);

should instead be:

UnityEditor.Animations.AnimatorController controller = UnityEditor.Animations.AnimatorController.CreateAnimatorControllerAtPath(“Assets/AC.controller”);

Hope this helps.