Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Manually attached AnimatorController won't play

Discussion in 'Animation' started by c0nstan, Feb 23, 2014.

  1. c0nstan

    c0nstan

    Joined:
    Jan 25, 2014
    Posts:
    6
    Hi there,
    i played around with animated Sprites according to this Tutorial: http://michaelcummings.net/mathoms/creating-2d-animated-sprites-using-unity-4.3#.UwnNf_l5OYI

    Everything works fine, and the sprite is animated. But for a real game i would want to load Sprites dynamically at runtime and attach animations to them.

    But that isn't working. Here is my code to create a GameObject, attaching a SpriteRenderer and Animator/AnimatorController to it.

    Code (csharp):
    1.        
    2.  var prof = Resources.Load("professor_walk_cycle_no_hat", typeof(Sprite)) as Sprite;
    3.  var profObject = new GameObject("Prof");
    4. profObject.transform.position = new Vector3(0, 0, -1); ;
    5. profObject.AddComponent<Animator>();
    6. profObject.GetComponent<Animator>().runtimeAnimatorController = (RuntimeAnimatorController)RuntimeAnimatorController.Instantiate(Resources.Load("professorAnimatorController"));
    7. profObject.AddComponent<SpriteRenderer>();
    8. profObject.GetComponent<SpriteRenderer>().sprite = prof;
    When i press play, the sprite is showing, but the animaton won't play.
    In the hierarchy window i can see that my GameObject looks identical to that, which i created manually.

    However the really strange thing is that, if i change a property in the Inspector (e.g. Apply Root Motion of the Animator Component), the animation starts to play correctly :/

    I found a similar problem here http://forum.unity3d.com/threads/210822-GameObject-won-t-move-when-I-attach-an-Animation-Controller

    Is that a bug in Unity, or is it basically not possible to load animated sprites at runtime?

    I cannot believe that adding all, possibly needed Sprites manually (with attached Animators to them), should be the way to go...
     
  2. c0nstan

    c0nstan

    Joined:
    Jan 25, 2014
    Posts:
    6
    Okay i found out the solution.
    Here is it for anyone who ran into the same problems.

    I have to add the SpriteRenderer Component BEFORE i add the Animator. I don't know why, but otherwise maybe the animation isn't bind to the sprite correctly? So here is the working code:

    Code (csharp):
    1.  
    2.         var prof = Resources.Load("professor_walk_cycle_no_hat", typeof(Sprite)) as Sprite;
    3.         var profObject = new GameObject("Prof");
    4.         profObject.transform.position = new Vector3(0, 0, -1);
    5.         profObject.AddComponent<playerController>();
    6.         profObject.AddComponent<SpriteRenderer>();
    7.         profObject.GetComponent<SpriteRenderer>().sprite = prof;
    8.         profObject.AddComponent<Animator>();
    9.         profObject.GetComponent<Animator>().runtimeAnimatorController = (RuntimeAnimatorController)RuntimeAnimatorController.Instantiate(Resources.Load("professorAnimatorController"));
    10.