- Home /
How to change color of animated sprite
I have an animated sprite that I'd like to change SpriteRenderer.color for in code. My code is fine, and will work when the animator component on the object in question is turned off. However, when the animator is on, the sprite's color gets reset to default white every frame for some reason. My animations aren't doing anything to the SpriteRenderer.Color, but it's still being changed. Does anyone have any suggestions as to why this might be happening?
Update: I have one animation that changes the color of the sprite. This animation is not the one playing when i attempt to change the color from code, but if it is in my animator tree (even if it's not connected to anything), the sprite's color will be modified by the animator. However, if I delete this animation or the color change in this animation, everything works fine. I don't understand why an unused animation that modifies SpriteRenderer.color being in my animator apparently makes every animation change SpriteRenderer.color.
Having the same issue here, any workaround besides disable the animator?
Answer by levpasha · Aug 08, 2016 at 12:46 PM
Find solution (you should turn off animator, change color, turn animator on):
RuntimeAnimatorController ac = gameObject.GetComponent().runtimeAnimatorController;
gameObject.GetComponent().runtimeAnimatorController = null;
gameObject.GetComponent().color = new Color(0.4f, 0.6f, 1f);
gameObject.GetComponent().runtimeAnimatorController = ac;
Work for me!
Answer by Kheremos · Oct 26, 2021 at 06:20 AM
You can also change the material color, like this:
GetComponent<SpriteRenderer>().material.color = new Color(0.2f, 0.3f, 0.8f);
It works for me. But I still wonder why I can't change SpriteRenderer.color when animator component is disabled. Besides, it seems not fix the real problem in this situation.
Your answer
Follow this Question
Related Questions
Making an object change color after Animation Ends 0 Answers
Efficient way to instantiate a sprite that plays an animation then destroys itself? (3d space) 1 Answer
Dragging sprite to create animation - doesn't ask where to save 1 Answer
add animation frames to existing spritesheet with animations? 0 Answers
how to display a single frame of a sprite animation 1 Answer