Animating Instantiated Objects in Unity

Andrea Zilio
4 min readMay 8, 2021

In an earlier post we looked at animating game objects. But what if it is an instantiated object. Going back to the space shooter example. Let’s add an animation like this:

Right now the enemy ships simply disappear when they are hit.

Setting up the animation

Our first step is to add the animation to the “enemy” ship. We already have the prefab. Lets open the prefab window and then add a new animation.

It works. However there is a loop and when we try to play the game, the animation will appear with the enemy ship. The loop can be removed on the animation from the inspector.

But why is the animation playing as soon as an enemy appears?

Controlling our animation in the animator

When we double-click the animator controller the animator opens up.

We see the animation is orange, which indicates it is the default behavior, upon entry. By right-clicking we add an empty state. Another right-click on the empty state allows us to set it as the default state. We can add a transition to the enemy destruction animation.This again is done with a right click on the Empty state. If we do nothing the animation will now appear immediately after the empty state.

To further control the transition we can add parameters. There are four types of parameters: float, int, bool and trigger. What we need it a trigger. I have called it OnEnemyDeath.

When selecting the transition (the blue arrow between Empty and Enemy_Destrution_anim), then you will see there are conditions in the inspector. With the plus we can now assign our trigger parameter OnEnemyDeath to the transition.

Writing the script

We now need to tell the game when to trigger the OnEnemyDeath. We do this in the enemy script.

We first create a handle for our animator:

private Animator _anim;

We then need to access the animator in the Start method. It is good practice to check if the player and the animator are actually there. Adding a Debug message helps to find the error, should there be one.

if (_player == null)
{
Debug.LogError("The player is NULL.");
}
_anim = GetComponent<Animator>();
if (_anim == null)
{
Debug.LogError("Animator is Null.");
}

We now set the trigger of the animation in the script where we control what happens when the enemy is hit.

_anim.SetTrigger("OnEnemyDeath");

To make sure the enemy stops where it is, once it is hit, we set the speed to zero. Here is the full method:

Have the animation start immediately

Once we run the game, we will see a delay before the animation starts. This can be avoided through a setting on the animation transition. Go back to Unity and select the transition from Empty to Enemy_Destruction_anim. In the inspector untick the “Has Exit Time”. Our animation will now start right away.

Everything should work nicely now …

Time to try it!

--

--

Andrea Zilio

Passionate Game Developer and Learning Expert. I love to create games and interactive experiences using Unity, Articulate, C#, JavaScript, PHP, HTML, CSS.