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

**URL:** https://discussions.unity.com/t/how-do-i-add-script-in-animatorcontroller-state-by-scripting/649902
**Category:** 2D
**Tags:** 2D
**Created:** [December 23, 2016, 3:12pm UTC](https://discussions.unity.com/t/how-do-i-add-script-in-animatorcontroller-state-by-scripting/649902 "2016-12-23T15:12:19Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Waaru](https://avatars.discourse-cdn.com/v4/letter/w/f1d935/32.png) [@Waaru](https://discussions.unity.com/u/Waaru)
#### Post date: [December 23, 2016, 3:12pm UTC](https://discussions.unity.com/t/how-do-i-add-script-in-animatorcontroller-state-by-scripting/649902/1 "2016-12-23T15:12:19Z")

</div>

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 ?

```csharp
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

---

<div class="post-metadata">

### Author: ![mikael\_juhala](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/mikael_juhala/32/288560_2.png) [@mikael\_juhala](https://discussions.unity.com/u/mikael_juhala)
#### Post date: [December 23, 2016, 9:35pm UTC](https://discussions.unity.com/t/how-do-i-add-script-in-animatorcontroller-state-by-scripting/649902/2 "2016-12-23T21:35:53Z")

</div>

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([t](http://unity3d.com/support/documentation/ScriptReference/30_search.html?q=MonoScript)ypeof(HandleMovementCollider));

---

<div class="post-metadata">

### Author: ![Waaru](https://avatars.discourse-cdn.com/v4/letter/w/f1d935/32.png) [@Waaru](https://discussions.unity.com/u/Waaru)
#### Post date: [January 2, 2017, 8:59am UTC](https://discussions.unity.com/t/how-do-i-add-script-in-animatorcontroller-state-by-scripting/649902/3 "2017-01-02T08:59:08Z")

</div>

> [@mikael\_juhala](#):
>
> 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([t](http://unity3d.com/support/documentation/ScriptReference/30_search.html?q=MonoScript)ypeof(HandleMovementCollider));

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.
