So, I have a sprite, I’ve animated it using the animation window > sprite renderer > sprite…
I added some keys made the animation, put it into an animation controller, added a default state, made some transitions with some conditions to play one of 2 animations, all this works fine.
After the animation finishes it goes back to the default state but I want to swap the sprite once the animation has finished…
So I added an event to call a method on the script attached to the object which calls the spriterenderer.sprite to try to set it to a referenced sprite but it won’t change?
I added a quick debug check to map it to the keypad so I can just swap the sprites from the keypad and it will not swap the sprites while the animator component is enabled…
If I disable the animator component in the inspector (while the scene is in play mode…or not) then I can swap the sprites but while it’s enabled it won’t change…So I know the code I’m using to swap the sprite from code is correct but it seems the animator is blocking it…
I found a question on stack overflow indicating this will not work at runtime and you need to change the sprite in the animator. Since I had a situation where I only wanted the animation at the end (For an explosion just before the shot object was destroyed) I found I could disable the animator in the prefab then just enable it just before triggering the animation. Works great
In my experience Animator wins out of any code, every time. If someone has found otherwise please let me know, as I’ve wrestled with from time to time for over a year now.
Side note: It would be great if Unity would introduce a way for script to win out over the animator. A special interface exposed by the animator perhaps that allows things like position, sprite setting, etc, to “win”.
One work around I’ve used is to parent the sprite in a game object and then show hide, squash, stretch, move, etc that parent, but never the sprite children. (In my experience, if you edit the sprite in any animation within a single animator it will override it for all code editing attempts.) You can then edit the sprite via code and have it perform as desired in animation. This is how I handle all skinning and equipment swap related stuff in my current project.
Is Write Defaults disabled on the animation clip? It could be that after the animation clip has finished it’s resetting it to it’s original sprite. Try turning Write Defaults off (if it’s not already) and see if that works. If not, you could try setting the sprite in an OnStateExit call of a StateMachineBehavior
I’ve had issue that animator was still using sprite in animations.
So the solution that I used was to create animation Ex. Idle1, Idle2. to default Animator controller I’ve added Idle1 motion and also created Animator override Controller with Idle2 as Idle motion and in script at runtime I’ve changed animator controller.
Try this only if if you didn’t succeed with previously mentioned solutions
You sir solved my problem. Toggling the Write Defaults Off from the main source layer did the trick!
For some reason my animator was resetting only the SpriteRendererColor Values of the current object. Everything worked smoothly, rotating angles, scaling, changing sprite source image but not the SpriteRenderer color values. They were constantly being reset after animation.
my solution is: if your animator do not play any animation that efforts to the game. you can first enable it false and change the value and reopen it to play next animations.
Hello,
im having alsow a problem with sprite change when a specific animation is added do animator controller. When i have only idle aniamation addet to animator, ship sprite is changed correctly from variable from scriptable object but when i add explosion animation that takes sprite change changes also back ship sprite to the “basic one”.
I turned off root motion and write defaults alsow and tried to disable animator and enable it after changing sprite, but no effects
LateUpdateWorks, but if i have other animations attached it could generate problem - I sopouse
I made a solution for this situation. I have addet an additional SPRITE RENDERER which is respocable for explosion animation. Think it’s the easiest way and alsow best perform.
Hey, I’d like to throw my hat in the ring for an alternative solution I found for this. It turns out, that even if you seemingly can’t get an animation clip to respect direct re-colors to the sprite renderer, you CAN get it to respect re-colors from another animation clip!
Basically:
1 Make an animation clip with exactly one purpose – re-color the sprite from bright to dark over the course of the entire animation.
2 Put that clip in it’s own layer in the animator, and set its’ speed to 0.
3 Put in a function to play that animation clip at a particular time. Run this function after changing the guy’s health.
private void ChangeSpriteColorForHealth()
{
//clamped to 0-0.99, because a value of 1 causes color anim clip to loop; is treated as 0 by unity.
float lerpHPVal = Mathf.Clamp(Mathf.InverseLerp(maxHealth, 0, health), 0, 0.99f);
animator.Play("Color.ColorValue", 0, lerpHPVal);
}
4 Huzzah! It works!
There’s a bit more information on my blog. I hope this helps someone!