Animating UI images on button click

Good afternoon, I made an animation for one of my UI images that work as icons.
It works but I have three problems:

  1. How can I use the same animation for the rest of the UI Images? I cannot see any “upload anim”.
  2. I have a script with a switch; each case represents a button clicked in my menu.
    So I want to run an animation when “X” button is pressed, another when “Y” button is pressed.
    How can I do? Is it possible to load an array of animators?
  3. Is there a function like Animator.Play()?

The problem is I have never animate any object and I sincerely don’t have a clue on how to do it.

Anyway, here’s my code:

    public Animator [] Icons;

    public int AnimToPlay = -1;                                            //It goes from 0 to 5 and indicates what animation has to be played when a certain button is triggered

    public void Start()
    {
        MyMusic = GetComponent<AudioSource> ();                            //Get the Audio Reference
    }

    public void ButtonClicked(RectTransform Clicked)                    //This recognizes which button I cliccked
    {
        Debug.Log ("The button clicked was " + Clicked.name);


        switch (Clicked.name)
        {
        case "btn_enroll":
            AnimToPlay = 0;
            PlayAnimations (AnimToPlay);

            ;
            break;

and PlayAnimations method:

void PlayAnimations(int Play)
    {
        Icons [Play].Play();                   //Here it says me it needs an argument; what string?
    }

I managed to get the animation working for one icon.

I added an Animation component to the object, set it and created a public Animator variable in my script.

Then with animator.GetComponent.Play(); I played the animation.

I added an Animation component to all my 6 icons and I dragged the same animation.
How can I add an Animator component to the other images so I can do animatorX.GetComponent.Play()?
If I simply add Animator as component, I can’t select my objects as conrollers; why?
Is it possible to create animators array as well?

Hmm. I don’t think I understand. Were you trying to find a simpler way to have all 6 of your icons play an animation from a single button press?

I guess your doing it in script, so the easiest way would be to grab the animator controller from the original Animator you were using and put in the controller of the Animator on the different buttons.

Note: This will only work if the buttons are named the same thing in the hierarchy.

Also You can grab an array of animators if you want. Your declaration in your current code is currently right. In the inspector for your script component, set the size of the array to 6 and add each button to the array.