- Home /
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);
}
}
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);
}
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.
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.
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
Follow this Question
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