I have created an empty GameObject and assigned it a script: Main.cs.

public class Main
    [MenuItem("Custom workflow/Initialize")]
    static void Initialize()
        // Place a text mesh on the scene
        var obj = new GameObject("Sample");
        obj.transform.position = new Vector3(0, 0, 0);
        var text = obj.AddComponent<TextMesh>();
        text.text = "Hello world";

        AnimationClip clip1 = CreateAnimationClip();
        AnimatorController sm = CreateSimpleAnimatorController(clip1);

        var animator = obj.AddComponent<Animator>();
        animator.runtimeAnimatorController = sm;
        animator.applyRootMotion = true;

    private static AnimationClip CreateAnimationClip()
        var translateX = AnimationCurve.Linear(0.0f, 0.0f, 2.0f, 25.0f);
        var animationClip = new AnimationClip();
        animationClip.SetCurve("", typeof(Transform), "position.x", translateX);

        AssetDatabase.CreateAsset(animationClip, "Assets/procanim.anim");

        return animationClip;

    private static AnimatorController CreateSimpleAnimatorController(
      Motion motion1 = null, Motion motion2 = null)
        // Creates the controller
        var controller = AnimatorController.CreateAnimatorControllerAtPath(

        // Add parameters
        controller.AddParameter("GoToB", AnimatorControllerParameterType.Bool);
        controller.AddParameter("GoToA", AnimatorControllerParameterType.Bool);

        // Add StateMachines
        var rootStateMachine = controller.layers[0].stateMachine;

        // Add States
        var stateA1 = rootStateMachine.AddState("stateA1");
        stateA1.motion = motion1;
        var stateB1 = rootStateMachine.AddState("stateB1");
        stateB1.motion = motion2;

        // Add Transitions
        var transitionAB = stateA1.AddTransition(stateB1);
        transitionAB.AddCondition(AnimatorConditionMode.If, 0, "GoToB");
        transitionAB.duration = 0;

        var transitionBA = stateB1.AddTransition(stateA1);
        transitionBA.AddCondition(AnimatorConditionMode.If, 0, "GoToA");
        transitionBA.duration = 0;

        return controller;

The problem

As I invoke the menu item, I get all assets in place. When I hit play, nothing happens. I can see state stateA1 is on, but the animation does not run. If I click on the text object in the scene...

enter image description here

...this is what I see in the animation window:

enter image description here

I think the problem might be here:

animationClip.SetCurve("", typeof(Transform), "position.x", translateX);

Because, when I remove the code generate Transform.position property from the animation timeline and manually add one via UI, it works once I hit play again.


The property name I was using is not correct. This will fix the issue:

animationClip.SetCurve("", typeof(Transform), "localPosition.x", translateX);

The list of properties that can be animated can be found in EditCurve's doc.

  • \$\begingroup\$ You might want to mark this answer as the accepted one :) \$\endgroup\$
    – Vaillancourt
    Jul 6 '20 at 14:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.