1
\$\begingroup\$

I've written a coroutine that starts an animation.

GameObject.FindWithTag(names[number]).GetComponent<Animator>().StartPlayback();

This is the line of code that I wrote to start the animation. However, the animation does not start. I've even replaced the GameObject.FindWithTag(String) method with a reference to the actual object by declaring a GameObject in the script and setting it to a particular object in Unity's IDE. The animation still didn't start. Also, I must say the GameObject does have an animator component, you can see it in the inspector panel. No error messages can be seen in the console during play mode. What can I do to get the animation to play when I want it to play.

enter image description here

\$\endgroup\$
4
  • \$\begingroup\$ You must instead use GetComponent<Animator>().Play(stateName, layer, normalizedTime) or GetComponent<Animator>().Play(stateNameHash, layer, normalizedTime) - Unity Docs for Play \$\endgroup\$
    – Nikaas
    Commented May 31, 2017 at 20:03
  • \$\begingroup\$ tried calling that on the animator component. Called anim.Play("light1",-1) becuase the name of the animation file is light1 and idk how to get the number representation of the base layer so I just put -1. That didn't work. What should the parameters be? \$\endgroup\$
    – Joel J.
    Commented May 31, 2017 at 20:52
  • \$\begingroup\$ Check the next tutorials and watch the whole section about Controlling Animation \$\endgroup\$
    – Nikaas
    Commented May 31, 2017 at 21:14
  • \$\begingroup\$ I went through those once. It didnt help me find a solution. maybe i missed something, ill watch again. \$\endgroup\$
    – Joel J.
    Commented May 31, 2017 at 21:21

1 Answer 1

1
\$\begingroup\$

Apparently on this site I don't have enough "rep" to comment. Animators should begin playing automatically, so the problem is not with how you're "starting" it. Ensure that

-The animator component is enabled

-The game object is enabled

-The state you want to play is the default

-The animator isn't transitioning to another state at the beginning

Barring that, if you are trying to explicitly tell the animator what state to play, you need to name the state, not the name of the animation file.

\$\endgroup\$
5
  • \$\begingroup\$ the animator and game object are enabled. the state name is the same as the animation file (shown in the picture above). How do I know whether or not the animator isn't transitioning to another state? \$\endgroup\$
    – Joel J.
    Commented Jun 1, 2017 at 20:33
  • \$\begingroup\$ does the entry state count, or does the transition need to be a state I created? \$\endgroup\$
    – Joel J.
    Commented Jun 1, 2017 at 21:04
  • \$\begingroup\$ The default state will be orange (like in the pic). Everything looks fine there. Try this, with your game object selected, press play and then look in the animator window. You should see a bar under the Light1 state showing that it is playing. \$\endgroup\$
    – Mike
    Commented Jun 2, 2017 at 4:19
  • \$\begingroup\$ I fixed the problem yesterday. I used an empty state as the default. and used the Play() method to start the desired animation. It worked \$\endgroup\$
    – Joel J.
    Commented Jun 3, 2017 at 1:13
  • \$\begingroup\$ @JoelJ. if you happen to come back, I recommend adding your solution as an Answer and accepting it. This makes the fix more visible to future users who have similar problems (and you can accrue up-votes for sharing a useful answer!) \$\endgroup\$
    – DMGregory
    Commented Sep 27, 2018 at 12:28

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .