- Home /
Why I cant use this?
I use this script to enable a SpriteRenderer:
character.GetComponent<SpriteRenderer>().enabled = true;
And it works well.
But when I create a Object with:
private SpriteRenderer sprite;
sprite = GetComponent<SpriteRenderer>();
But if I use this don't works, why?
Answer by jihadkhawaja · Dec 21, 2019 at 01:47 AM
@clcriado0 it’s because if the sprite component is already disabled it can’t be enabled back from the component because the sprite component itself is disabled, but character.getcomponent().enabled; is finding the component reference again even if it’s disabled and then enabling it that’s why it won’t work character.sprite.enable = true. U can disable the sprite using character.sprite.enabled = false if it was enabled but not otherwise. (Sorry for any typos i am using mobile).
I only want to know why this works:
character.GetComponent().enabled = true;
And why if i create an Object of SpriteRenderer called sprite and use it dont works:
private SpriteRenderer sprite;
sprite = GetComponent();
character.GetComponent().enabled = true;
THIS WOR$$anonymous$$S: character.GetComponent().enabled = true;
THIS DONT WOR$$anonymous$$: character.sprite.enabled= true;
Your answer
![](https://koobas.hobune.stream/wayback/20220612220444im_/https://answers.unity.com/themes/thub/images/avi.jpg)