• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Voenskjall · Apr 01, 2015 at 04:18 PM · animationanimationclipconvert

Convert AnimationClip to Component

Hello!

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!

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Voenskjall · Apr 04, 2015 at 10:20 AM 0
Share

Sorry, I still haven't found any solution, please let me know if there's any important information I forgot to mention

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by dorpeleg · Apr 05, 2015 at 03:00 PM

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.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Voenskjall · Apr 06, 2015 at 01:42 PM 0
Share

Thank you dorpeleg, I'm going to try what you wrote, and reply!

avatar image Voenskjall · Apr 07, 2015 at 02:38 PM 0
Share

Sorry dorpeleg, you said

Then, assign the clip in the inspector manually.

But this is what I want to avoid, assigning clip manually. I would like to assign the clip with a script. Thank you for your help, I believe it is the answer I need, I just did something wrong and now don't know what to do or how to achieve it.

avatar image dorpeleg · Apr 07, 2015 at 06:58 PM 0
Share

What you are trying to avoid is assigning it to each object no?

$$anonymous$$y solution only makes you assign it once and will apply to all objects.

But, you can also use Resources.Load to avoid manual assigning.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Problems sampling animation in editor 0 Answers

how to make multiple objects move in sequence 0 Answers

Play animation OnTriggerEnter (C#) 1 Answer

5.4.0f3 Animation clip inspector changes have caused clips to slide. 0 Answers

Is there a way to abstract sprite from animation clip? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges