hide animated Sprite by code (SpriteRenderer)

i am trying to hide sprite (SpriteRenderer component) which is animated by mecanim using animation in unity (it’s not spritesheet animation, it’s animated sprite gameobject)

i can’t find way how to hide it by code

when i use

GameObject.Find("gameobjectname").GetComponent<SpriteRenderer>().enabled = false;

it’s not affecting the sprite at all, i can’t even click on the component checkbox to disable it in runtime

when i use

GameObject.Find("gameobjectname").GetComponent<SpriteRenderer>().color = Color.red;

then it colors the sprite to red

any ideas what i do wrong or is it just impossible to hide object which is animated?

i maybe solved it, animator changed name of sprite attached in SpriteRenderer, when i renamed gameObject to same name as sprite, it started to work

however this is bug in unity, because coloring works with different name

Update:

only way I achieve what i need now is to colorize the sprite by color with zero alpha because if i need animate the sprite then i actually switch the sprites attached in the sprite renderer … however it’s general inconsistency i guess, when i call component on gameObject it should work same way with all properties

Hi, You can use the codes listed here.

private SpriteRenderer sprite_renderer;
void Update()
{
     sprite_renderer = GetComponent<SpriteRenderer> ();
     sprite_renderer.enabled = false;
}

Thanks