Search Unity

  1. We are migrating the Unity Forums to Unity Discussions by the end of July. Read our announcement for more information and let us know if you have any questions.
    Dismiss Notice
  2. Dismiss Notice

Question Runtime added animation stops at frame 1

Discussion in 'Animation' started by Napoleonite, May 11, 2024.

  1. Napoleonite


    Mar 15, 2015
    Code (CSharp):
    1. // Code inside the Update() method of some spawner:
    2. GameObject obstacleGameObject = new("Obstacle");
    4. // <snip: add SpriteRenderer and such here>
    6. // Use an Animator.
    7. Animator animator = obstacleGameObject.AddComponent<Animator>();
    8. animator.runtimeAnimatorController = obstacleData.AnimatorController; // Get AnimatorController from ScriptableObject called: obstacleData.
    9. animator.speed = 1f; // shouldnt be required.
    10. animator.updateMode = AnimatorUpdateMode.Normal; // shouldnt be required.
    After it spawned, it plays only 1 frame of the animation and then stops. If I duplicate the newly spawned gameobject in the scene-view then the duplicated version works but the original one doesn't... I don't get it?

    Must I refresh the Animator component or something?

    UPDATE: Nevermind, shortly after posting this I finally found the problem. A coroutine was setting the animation speed to 0..
    Last edited: May 11, 2024