- Home /
Question by
motaka666marufp876 · Aug 14, 2021 at 09:56 PM ·
spriterendererdisableontriggerstay
Disable player's spriteRenderer as long as player stays there
I want to make a hiding spot that when the player get into it, player's SpriteRenderer should disable but when he get out of it Sprite Renderer should re-enable again . I tried "OnTriggerStay2D" but when i disable the spriteRenderer it disabled but never re-enabled again despite i leave trigger spot. Any Suggestions ? thanks
void OnTriggerStay2D(Collider2D collider) { if (collider.gameObject.tag == "Bushe") { collider.gameObject.GetComponent().enabled = false; } }
Comment
Best Answer
Answer by mak431020 · Aug 15, 2021 at 06:59 AM
You can do something like this
void OnTriggerEnter2D(Collider2D collider)
{
if (collider.gameObject.tag == "Bushe")
{
collider.gameObject.GetComponent().enabled = false;
}
}
void OnTriggerExit2D(Collider2D collider)
{
if (collider.gameObject.tag == "Bushe")
{
collider.gameObject.GetComponent().enabled = true;
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613041654im_/https://answers.unity.com/themes/thub/images/avi.jpg)