I have an "Enemy" Animator Controller:
The enemy dead clip disables the Sprite Renderer after 4 frames:
The EnemyDamaged clip just moves position and rotation for a few frames:
For my green blob, I've set up an override controller that overrides the above clips with a series of sprites:
So, a Blue or Red blob uses the standard "Enemy" animator controller that only turns on/off the SpriteRenderer, but does not change the sprite. The Green slime uses the Green Slime's "Override" animator controller with the override clips that do change the sprites.
The following code runs on my Enemy.cs script to spawn a new monster. When a Green slime dies (the only enemy with an override animator controller), the next spawned enemy always has the base green sprite, but if a red or blue slime dies the next enemy's sprite is updated correctly.
public void Initialize(Monster monster)
{
animator.ResetTrigger("Dead");
animator.ResetTrigger("Damaged");
health = monster.health;
maxHealth = monster.health;
attack = monster.attack;
defense = monster.defense;
this.monster = monster;
enemyNameText.text = monster.name;
spriteRenderer.sprite = monster.sprite;
currentHealthTransform.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, maxHealthTransform.rect.width * (float)health / maxHealth);
animator.runtimeAnimatorController = monster.animatorController ? monster.animatorController : defaultAnimatorController;
animator.SetTrigger("Spawned");
}
If I change the code to the following to only ever use the defaultAnimatorController for all enemy types, the issue disappears, but I can no longer override my animations:
public void Initialize(Monster monster)
{
animator.ResetTrigger("Dead");
animator.ResetTrigger("Damaged");
health = monster.health;
maxHealth = monster.health;
attack = monster.attack;
defense = monster.defense;
this.monster = monster;
enemyNameText.text = monster.name;
spriteRenderer.sprite = monster.sprite;
currentHealthTransform.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, maxHealthTransform.rect.width * (float)health / maxHealth);
animator.runtimeAnimatorController = defaultAnimatorController;
animator.SetTrigger("Spawned");
}
Here's a GIF of the bug happening. Notice how after the green slime dies, the sprite stays green despite it spawning a red slime and having the original "Enemy" animator controller that does not affect the SpriteRenderer.
If you're interested in tinkering with the code yourself, here's a link to a github repo of that produces this bug.
Thanks to DerHugo's comment, I was able to identify the issue.
If the Green Slime controller is active while trying to set the SpriteRenderer's .sprite property, that line of code is ignored because changes to the .sprite property are controlled in one of its animation clip overrides.
If the Default controller is active while trying to set the .sprite property, the line of code executes without problem.
The base sprite updates correctly after Red and Blue slimes die because the Default controller is active (not binding the property). The base sprite does not update correct after a Green slime dies because the Green slime's override controller is active (binding and blocking the property).
public void Initialize(Monster monster)
{
animator.ResetTrigger("Dead");
animator.ResetTrigger("Damaged");
health = monster.health;
maxHealth = monster.health;
attack = monster.attack;
defense = monster.defense;
this.monster = monster;
enemyNameText.text = monster.name;
currentHealthTransform.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, maxHealthTransform.rect.width * (float)health / maxHealth);
animator.runtimeAnimatorController = defaultAnimatorController;
spriteRenderer.sprite = monster.sprite; //Moving this line here fixes the problem for the default animator controller
animator.SetTrigger("Spawned");
}