Resources.Load GameObject is missing animations

Hi All,
if I use Resources.Load to load a gameobject (prefab ) the object loads fine, however when I try to grab the animation it’s missing. I can clearly see the prefab has an animation attached. When I step through the code I can see the object is loaded, but when I ask for the animation I get a null animation with this error string.

“MissingComponentException:There is no ‘Animation’ attached to the "MonsterSymbol" game object, but a scr…”

what I do

        GameObject tempobj = Resources.Load( animation_name, typeof( GameObject ) ) as GameObject;


        if( tempobj == null )
        {
            Debug.Log("<color=red>Error:</color> Missing GameObject symbol " + animation_name );
        }
        else
        {
            Animation anim = tempobj.GetComponent< Animation >( );
            moster_animation.animation_controller = anim;
        }

Can anyone advise why the animation isn’t getting loaded with the gameobject and I can make this happen?
Thanks

Should add these are 2d sprite animations

I’m not 100% certain with this, but it looks like in line 12 you are trying to assign the animation that you loaded from the resources directly into the animation_controller field of the “moster_animation” class. I assume the animation_controller field is an Animation_Controller class, does assigning an animation into that even work?
If it does, could it maybe mean you are missing an Animation component on the “moster_animation” object, not on your tempobj?

1 Like

Thanks you’er right. Animation controller. Still have an issue directly linked to that, I’m trying to link to a script able object, basically I can write an animation to a database. Maybe just need to use string name and load.

I have never tried loading animations myself, but in theory it should work if you just store the name in the database and load it from Resources as Animation, just as you loaded the tempObj in your initial problem.
Before putting 100s of animations into the Resources folder, you could consider using AssetBundles instead. Unity itself discourages the usage of the Resource folder in its “Best Practices on Assets & Resources”

1 Like