- Home /
Add animation to a GameObject on runtime via script
Currently I'm trying to implement dynamically spawning GameObjects via script on runtime. For example, I have a file exported from 3ds max called spaceanomaly.FBX. I've also exported the animation with it. When viewing the imported asset in unity the animation shows up as a subobject of said asset (with the name "animation").
When creating the GameObject via Instantiate the object properly spawns with all meshes and textures. However the animation seems to be missing (although as I said, it shows up as sub-object). This means GameObject.animation is null at the beginning. I solved this part already by using:
obj.AddComponent("Animation");
However, the animation is still empty. Upon running
obj.animation.Play();
or with the name of the animation:
obj.animation.Play("animation");
Nothing happens, respectively with the 2nd variant I get an "could not be played because it couldn't be found". Now my question is, how do I tell unity to load the animation that is a sub-object of the asset?
Answer by Yamineko · Mar 21, 2014 at 04:56 PM
Solved it myself, the problem was that the object itself didn't have the "Animation Type" field (in Rig, object inspector) set to "Legacy". Animation is properly added upon Instantiate now.
Answer by nonehill · Apr 09, 2015 at 06:16 PM
Check this out awesome example https://github.com/nonehill/DynamicallyLoadingAnimation It reduce memory usage in my 2d game from 800mb to 120 mb in runtime. Load and Unload animations whatever you need at runtime
Answer by nonehill · Apr 09, 2015 at 06:16 PM
Check this out awesome example https://github.com/nonehill/DynamicallyLoadingAnimation It reduce memory usage in my 2d game from 800mb to 120 mb in runtime. Load and Unload animations whatever you need at runtime
Your answer
Follow this Question
Related Questions
Asset store tools broken? 1 Answer
How to make the new FP-Character react to forces? 1 Answer
Asset colors not imported correctly 1 Answer