2D animation sprite renderer not updating

I’m currently making an animation using Unity’s animation editor, for an enemy who will draw (as in draw a weapon, not draw a picture) a dagger. The enemy is a gameObject, and the dagger is a child gameObject of that enemy (with its own sprite renderer).

102298-dagger-gameobject.png

Initially, the dagger’s sprite renderer is off, but I turn it on during the “drawdagger” animation, like this:

What happens here is that the sprite renderer gets enabled only for the duration of the animation. So I made a function (that gets called when the enemy code tells the enemy’s animator to play that animation) whose job is to manually set the sprite renderer to be enabled through a reference to a gameObject. Like this:

public SpriteRenderer daggerSprite; // Reference set in inspector
//...
// Then, when it's time to play the animation:
animator.Play("drawDagger");
daggerSprite.enabled = true;
// I also have a counter here that counts down while the animation is playing

The animation plays but the dagger’s sprite renderer only gets enabled for a split second while the animation is playing - when the animation finishes, the dagger’s sprite renderer gets disabled again.

The code is being called, but the sprite renderer doesn’t seem to listen. I also tried making daggerSprite into a GameObject and calling:

daggerSprite.GetComponent<SpriteRenderer>().enabled = true;

But that didn’t work either. I know the reference is set correctly because I click the referenced object and it highlights appropriately in the scene’s hierarchy.

I also tried setting the animation counter to different values, since I thought that maybe it was looping for a split second (and somehow resetting the sprite renderer to be disabled), but this doesn’t seem to be the case either.

Weirdly, when I inspect the dagger gameObject in play mode, I can’t enable or disable its sprite renderer - clicking the checkbox has no effect.

Also, the same thing happens “the other way around”, i.e. if I initially set the dagger’s sprite renderer to be enabled, and change the animation and code to disable it, the sprite gets disabled for a split second and then comes back. This is what I mean when I say it’s like the sprite renderer just isn’t listening to code commands. This also means it can’t be the case that something elsewhere in some code is manually overriding it to be enabled or disabled.

I’m not very experienced with animating in Unity (or at all), so I guess what I want to ask is:

  1. Is there a better way of approaching the initial problem (of having the dagger’s sprite renderer be enabled during a “draw dagger” animation and then stay enabled)

  2. If not, what can I do about the current implementation’s problems?

Thanks!

I believe the correct way (take it with a grain of salt though) is to have separate animations based on what is equipped. So, you’d go from idle_unarmed to draw_dagger to idle_dagger. That way idle_unarmed can make sure the dagger is disabled while idle_dagger can make sure it’s enabled.