3
votes

I'm making 2D platformer game using Unity 4.3.4 engine. I've created a simple prefab, which have two animations: "idle" and "death"(i used "animator") and script to control this animations.

And here's the problem: when i instantiating clones of this prefab, they always show "idle" animation and don't turn on "death" when needed.

pos = new Vector3 (-5, 4, 0) * TileSize; 
newObject = Instantiate (Bonus, pos, Quaternion.identity) as GameObject;

But what is insteresting: i found a way to make animation work fine. Just add after instantiation one string like this:

newObject.animation["boxNew"].speed=1;

or this(or any string that trying to operate with "animaton"):

newObject.animation.enabled=true;

Of course i get exception at this string: "MissingComponentException: There is no 'Animation' attached to the "BonusBlock(Clone)" game object" . This is true, i really don't have Animation component, i have Animator. But why everything is working this way? Can anyone explain this?

2

2 Answers

0
votes

The animator is really only used if your going full blast with the new mecanim/state flow animator, what you prolly want to do for a simple example would be to add a Animation (not animator) component to your prefab, and assign your 2 animations to the animations list in the inspector for the said animation component. Afterwards on the object you could use newObject.animation.Play("death"); to play the death animation when you want it to trigger. or use something like newObject.animation.CrossFade("death"); for a blended animation.

Found here -> http://docs.unity3d.com/Documentation/ScriptReference/Animation.html

0
votes

Unity Technologies gave the answer: Before deactivating, use a default state to reset the gameObject. Discussion on Unity3d Forum

If you can read chinese, click here