Search Unity

Resources.Load GameObject is missing animations

Discussion in 'Editor & General Support' started by knobby67, Apr 21, 2018.

  1. knobby67

    knobby67

    Joined:
    Aug 30, 2015
    Posts:
    389
    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
    Code (csharp):
    1.  
    2.         GameObject tempobj = Resources.Load( animation_name, typeof( GameObject ) ) as GameObject;
    3.  
    4.  
    5.         if( tempobj == null )
    6.         {
    7.             Debug.Log("<color=red>Error:</color> Missing GameObject symbol " + animation_name );
    8.         }
    9.         else
    10.         {
    11.             Animation anim = tempobj.GetComponent< Animation >( );
    12.             moster_animation.animation_controller = anim;
    13.         }
    14.  
    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
     
    Last edited: Apr 21, 2018
  2. OneManBandGames

    OneManBandGames

    Joined:
    Dec 7, 2014
    Posts:
    207
    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?
     
    knobby67 likes this.
  3. knobby67

    knobby67

    Joined:
    Aug 30, 2015
    Posts:
    389
    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.
     
  4. OneManBandGames

    OneManBandGames

    Joined:
    Dec 7, 2014
    Posts:
    207
    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"
     
    knobby67 likes this.