Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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 maggot · Oct 23, 2011 at 12:33 PM · c#animationclipadding

Add an animation clip with AnimationClip using cSharp script

How do I assign an animation clip to a game object in cSharp ? I have a class called myThirdPersonController with declarations of AnimationClip as follows : public AnimationClip idleAnimation; // etc

and I want to add to idleAnimation my idle animation as done in the Editor by adding a clip, but through a script called StartUp accessing myThirdPersonController script. Here is my StartUp.cs script that I am using to access myThirdPersonController.cs : public class StartUp : MonoBehaviour {

 void Start() {

     GameObject example = (GameObject)(Resources.LoadAssetAtPath("Assets/Models/character.FBX", typeof(GameObject)));

     example.AddComponent("myThirdPersonController");

     myThirdPersonController myCharacterController001 = example.GetComponent<myThirdPersonController>();

     // set up animation clips here : clips are idle,walk,jump,run

     

     CharacterController myCharacterController002 = example.GetComponent<CharacterController>();

     myCharacterController002.center = new Vector3(0,1,0);

     Instantiate(example, new Vector3(0,0,0), Quaternion.identity);

 }

}

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by SteveFSP · Oct 23, 2011 at 01:30 PM

If I understand your question, it should be pretty easy:

// Where idleClip is an AnimationClip.
Animation anim = example.AddComponent<Animation>();
anim.AddClip(idleClip, idleClip.name);

[Updates based on comments]

I don't know of a way to query and extract animation clips directly from an FBX import. But, if creating a game object directly from an FBX behaves like a normal FBX import, then the game object will contain an animation component.

You can add this code snippet to test to see if an animation component is present on the game object.

Animation anim = example.animation;
if (anim == null)
{
    Debug.LogError("Sorry, no animation component.  This process won't work.");
    return;
}

If an animation component was created by the FBX import process, then you can use the following process to extract the clips.

Animation anim = example.animation; AnimationState state = anim["idle"]; if (state == null) Debug.LogError("Missing idle animation."); else { myCharacterController001.idleAnimation = state.clip; }

state = anim["run"]; // Continue with the rest of the animations. // ...

A final note: You can use this code snippet to examine the clips in an Animation component.

Animation anim = example.animation;
foreach (AnimationState state in anim)
{
    if (state != null)
        Debug.Log("Found clip: " + state.clip.name);
}
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 maggot · Oct 23, 2011 at 03:16 PM 0
Share

Its the AnimationClip that I have difficulty adding with script. I have added clips to the FBX object, one of them is called idle. I can add idle to the animationClip called idleAnimation(or Idle Animation as its displayed in the inspector) at runtime, but I can't set AnimationClip to idle for IdleAnimation using scripts. $$anonymous$$aybe its because I need to set the AnimationClip idleAnimation to element 1 of my FBX model animation component.

avatar image SteveFSP · Oct 23, 2011 at 05:11 PM 0
Share

O$$anonymous$$. I think I get what you mean. Is this correct?

The FBX contains animation clips. You want to access these clips and assign them to a field in $$anonymous$$onobehaviour component. In this case: myThirdPersonController.

I'll update the answer to address that.

avatar image maggot · Oct 23, 2011 at 05:44 PM 0
Share

Correct:) myThirdPersonController is the name of the script which contains what I want to do with the animation clips. Currently in myThirdPersonController.cs I have these declarations to store animation clips :

public AnimationClip idleAnimation; public AnimationClip walkAnimation; public AnimationClip runAnimation; public AnimationClip jumpPoseAnimation;

StartUp.cs is the script which accesses myThirdPersonController, and I want it to take my definitions for the animation clips as I set up in the animations component with inspector, and apply them to the gameObject.

Even without referencing the animation component would be fine, initializing the animation clips for the gameObject from startUp.

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.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Animation clip does not exist? 1 Answer

Adding animation transition between idle and walk 1 Answer

How to give a speed to random spawned object? 1 Answer

Adding a box collider to an object in csharp script 3 Answers

Animation / Distance 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