Convert AnimationClip to Component


I have an animation flipAnim.anim.

The first C# Script generates a fixed amount of GameObject, named emptyPoint.
I would like to add flipAnim to every emptyPoint.

If I’m right, this can be done like this:

Animation flipAnimation = emptyPoint.AddComponent<Animation>();

There is second C# Script. This script is attached to every emptyPoint GameObject. If I click any children of emptyPoint (while in Play Mode), it should play the animation flipAnim. Here is the code:

void OnMouseDown () {

		transform.parent.GetComponent<Animation>().Play ("flipAnim");


This only works when I select all the generated emptyPoint GameObjects in Play Mode, and assign the animation flipAnim manually.

But I would like to set the animation flipAnim via code/script.
And for this, I need both Animation and AnimationClip.

Let’s return to the first C# Script.
I need to add a new AnimationClip, then specify the animation’s name.

Now my code looks like this:

                    Animation flipAnimation = emptyPoint.AddComponent<Animation>();
					AnimationClip flipClip = emptyPoint.AddComponent<AnimationClip>();
					flipAnimation.AddClip(emptyPoint.GetComponent<AnimationClip>(), "flipClip");

The second line gives an error:
Assets/Scripts/BuildMap.cs(58,77): error CS0309: The type UnityEngine.AnimationClip' must be convertible to UnityEngine.Component’ in order to use it as parameter T' in the generic type or method UnityEngine.GameObject.AddComponent()’

I couldn’t find any answer, and I tried a lots of other codes, samples, but the problem is still there.
Thank you!

AnimationClip is not a component and cannot be converted to component.

In the first C# Script have a public var like this:

public AnimationClip clip;

Then, assign the clip in the inspector manually.

Next, when adding the Animation component, do like so:

Animation flipAnimation = emptyPoint.AddComponent<Animation>();
flipAnimation.AddClip(clip, "flipClip");

That should work.