How do I add animation to an empty object using just C#?

Hi I have a working FBX called “sakuyaflip”

And I wanted to know how I would add it to an empty object without using the inspector to do anything except: Add Compnent> New Script> GetSakuyaFlipFBX

using UnityEngine;
using System.Collections;

public class GetSakuyaFlipFBX : MonoBehaviour {

    void Reset () {
        Animation myAnim = gameObject.GetComponent<Animation>();
        if ( myAnim == null ) myAnim = gameObject.AddComponent<Animation>();
        Object[] objs = UnityEditor.AssetDatabase.LoadAllAssetsAtPath( "Assets/##PATH##/sakuyaflip.fbx" );
        foreach ( Object o in objs ) {
            if ( o.GetType() == typeof( AnimationClip ) ) {
                //Optinally:
                //if ( o.name == "Take 001" )
                myAnim.clip = (AnimationClip) o;
                break;
            }
        }
    }
}

Thanks for the code, and there are no errors with it, but when I hit play the empty object does not draw the FBX, it remains invisible.

You only said add animation.
Animation is not mesh.
Put in something like
(if o.gettype == typeof(Mesh)) addComponent().mesh = (Mesh) o
addComponent()
And get rid of the break.

Can I ask why you’re going about this the hardest possible way? Rather than “adding it to an empty object”, you could be simply instantiating the object (when a FBX file is imported, it is imported as, essentially, a prefab).

@ StarManta, because I don’t want the object for this “character” to be attached to a model. I want to make it so that the character can have 1 of a variety of outfits.

@ hpjohn, You are correct, my mistake, I wasn’t aware that you could have an animation exist without meshes to animate. Now I know, thank you. I appreciate the your being patient with me on this, but may I ask that you provide it in the way of your first response? I’ve been trying to get your second code to fit into the first for 20 minutes now, and there are a variety of things I don’t understand how to fix, from errors in the console, red squiggly lines under addComponent, and some words appearing in beige.

Yeah that was typed out half-assedly, I figured you could extrapolate, since it’s exactly the same checks.
Note: This is only for a single mesh, if your animation is skeleton animation for a rigged character, you need to learn how to mecanim.

    void Reset () { //Reset fires when component is added, or reset via inspector context menu
        Animation myAnim = gameObject.GetComponent<Animation>();
        if ( myAnim == null ) myAnim = gameObject.AddComponent<Animation>();
        MeshFilter myMeshFilter = gameObject.GetComponent<MeshFilter>();
        if ( myMeshFilter == null ) myMeshFilter = gameObject.AddComponent<MeshFilter>();
        MeshRenderer myRenderer = gameObject.GetComponent<MeshRenderer>();
        if ( myRenderer == null ) myRenderer = gameObject.AddComponent<MeshRenderer>();

        Object[] objs = UnityEditor.AssetDatabase.LoadAllAssetsAtPath( "Assets/Man/Man.fbx" );

        bool foundAnimation = false;
        bool foundMesh = false;
        foreach ( Object o in objs ) {
            if ( o.GetType() == typeof( AnimationClip ) && !foundAnimation ) {
                //Optinally:
                //if ( o.name == "Take 001" ) {
                    myAnim.clip = (AnimationClip) o;
                    foundAnimation = true;
                //}
            }
            if ( o.GetType() == typeof( Mesh ) && !foundMesh ) {
                //Optinally:
                //if ( o.name == "Hat Mesh" ) {
                    myMeshFilter.mesh = (Mesh) o;
                    foundMesh = true;
                //}
            }
        }
    }

Then you’ll want to instantiate it the same way, but replace meshFilter.mesh with your new outfit’s mesh.

Your mindset seems to be that the mesh is intrinsically tied to the GameObject, while the animation is a bit that you add on after the fact. If anything, the opposite is true. The mesh is one variable on one component, while the animation component controls a huge number of Transforms that are children of the GameObject - and that you would have to recreate manually if you were to simply add the animation component after the fact. (the “optimize game objects” option reduces this dependency on child objects, but still does not eliminate it, especially if - as I suspect - you need to expose any transforms for interaction, in which case you’re right back where you started).

If you’re importing an FBX, use the prefab-like object it provides, period. Otherwise you’re in for a world of hurt. Replacing the mesh on the animation is hundreds of times easier than replacing the animation on a mesh.

@ StarManta, what exactly did you have in mind originally? How exactly would your original solution to this issue work?