How to turn on animation from script

It’s must be as simple as Animation.Play(), right?
What I want: GameObject (“Shell”) instantiated and fly towards the target. When it’s reached target, it change shell’s sprite to chain of explosion sprites, then Destroy ()
What I have : gameObject “shell” with:

  1. Sprite Renderer (with sprite
    “shell”)
  2. shell_script
  3. Animator

Animator have “ProjectileShell” Controller, which contain “explosion” animation

problem is animation is played by default, and Ican not figure out how to get to it thru script. Add animation straight to GameObject is Legacy and, more than that, i can’t get it working too(

if you want to use Animator component (Mecanim), you need to:

  1. create Animation Controller - in top menu Assets / Create / Animation Controller

  2. attach the AnimationController to the Animator Component

  3. open window Animator - in top menu Window / Animator

  4. select the gameObject with the Animator (AnimatorController attached) and in Animator window you see state machine scheme (actually no scheme now, the AnimatorController is empty, but you can have it there)

  5. drag animations (made for the particular gameObject) to the Animator window and you will make states from the animations this way

  6. name states as you wish (not bad idea to leave them with the same name as animations)

you can pass variable to Animator (Mecanim) and start transitions in it):

or you can call directly from code:

You need to do:

Animator anim = gameObject.GetComponent<Animator>();
anim.Play("yourAnimationStateName");

But if the default state is your required animation state, then your animation will be played automatically since thats the default state of animation

28509-anim1.png

So create an idle state in Animator and Set as Default(Yellow Color) and then make transition to your required animation state from idle, based on a trigger/bool/float/int parameters.

else you can directly Play the animation as well without parameters by doing the above. But using those parameters are helpful.