How do i add script in animatorController.State ? by Scripting

Hello,

I want to add behavior and in this behavior add a script, but i can’t use the methode AddStateMachineBehaviour.
I already script the creation of my animator controller and my state with my motion (“be_hit.anim”) but i can’t add my script (HandleMovementCollider.cs) in the state.
Can anyone help me use this method or use anything else that can do the same thing ?

AnimationClip test = AssetDatabase.LoadAssetAtPath("Assets/Animations/Test/be_hit.anim", typeof(AnimationClip)) as AnimationClip;
MonoScript test2 = AssetDatabase.LoadAssetAtPath("Assets/Scripts/HandleMovementCollider.cs", typeof(MonoScript)) as MonoScript;
var controller = UnityEditor.Animations.AnimatorController.CreateAnimatorControllerAtPath ("Assets/Animations/test.controller");

var Locomotion = controller.AddMotion(test); // don’t add an entry transition, should entry to state by default
Locomotion.AddStateMachineBehaviour(MonoScript); //This doesn't work

Sorry for bad english

MonoScript is a base class for script assets, so there’s no way for Unity to know which script you want to add. Instead of loading the asset, you should use your script’s type (HandleMovementCollider).

Try this:
Locomotion.AddStateMachineBehaviour(typeof(HandleMovementCollider));

1 Like

Hi,

I thought it would work but it seams that HandleMovementCollider is not recognize and if i try to add handlemovement as a variable it’s the same thing. I will try a different way, maybe if i clone my animator and change the behavior it will work.