d d doe
d d doe

Reputation: 125

how to disable a sprite with SetActive(false)

I have a simple code that shows a sprite when player enters a trigger:

ps: the sprite isn't on a GUI

void OnTriggerEnter(Collider other)
{
    if (other.gameObject.tag == "Player")
    {
        ItemCard.SetActive(true);
    }
    else
    {
        ItemCard.SetActive(false);
    }
}

but my SetActive(false) didnt work and the sprite still showed at display. Im forgeting something?

Upvotes: 0

Views: 1328

Answers (2)

Technivorous
Technivorous

Reputation: 1712

im going to go out on a limb here and guess, that what your trying to do is show a card when your in the trigger and make it it go away when you leave?

there is a method called OnTriggerExit for that. try this:

void OnTriggerEnter(Collider other)
{
    if (other.gameObject.tag == "Player")
    {
        ItemCard.SetActive(true);
    }

}

void OnTriggerExit(Collider other)
{
    if (other.gameObject.tag == "Player")
    {       

        ItemCard.SetActive(false);
    }

}

the reason i say this, is because your method will only set the object to inactive if the collider that enters it is NOT the player. i could be wrong maybe thats what you want but if so the else is unneeded. good luck! lemme know if this works for you!

with your original method you are basically saying:

if a player walks in show card, if anything except a player collides hide card

so you would need something else to collide with it to make it go away

Upvotes: 5

DangerMouse
DangerMouse

Reputation: 128

What is ItemCard? The following works for me:

public class TestClass : MonoBehaviour
{
    public GameObject dummy; // this is a sprite

    void Start()
    {
            dummy.SetActive(false);
    }
}

Upvotes: 0

Related Questions