GameObject not colliding properly with a collider!

Hi! So I’ve got a gameObject that’s colliding with a box collider! When this happens there is a canvas that’s activating a text! Everything is okay till the moment when the two objects have to collide with each other… When the gameObject collide with the box collider the text I want to appear is doing it but only for a moment… Here is what I mean Gif

and this is my code for the gameObject

   public Text text;
    public GameObject panel;

    void Start()
    {
        text = GameObject.Find("sewingCanvas/GameObject/Panel/Text").GetComponent<Text>();
        panel = GameObject.Find("sewingCanvas/GameObject/Panel");
        panel.SetActive(false);
        text.gameObject.SetActive(false);
    }

    void OnTriggerEnter(Collider co)
    {
            if (co.tag == "archieoPlaced")
        {
            panel.SetActive(true);
            text.gameObject.SetActive(true);
            text.text = "Правилно! ";
        }
        else if (co.tag == "shopPlaced" || co.tag == "schoolPlaced" || co.tag == "hospitalPlaced" || co.tag == "fitnessPlaced" || co.tag == "micPlaced" || co.tag == "shoesPlaced" || co.tag == "policePlaced" || co.tag == "firePlaced" || co.tag == "archPlaced" || co.tag == "programmerPlaced")
        {
            panel.SetActive(true);
            text.gameObject.SetActive(true);
            text.text = "Грешка! Опитайте с друг предмет ";
        }

        else
        {
            panel.SetActive(false);
            text.gameObject.SetActive(false);
        }

    }

    IEnumerator OnTriggerStay(Collider co)
    {

        yield return new WaitForSeconds(5);

        if (co.tag == "schoolPlaced" || co.tag == "hospitalPlaced" || co.tag == "fitnessPlaced" || co.tag == "micPlaced" || co.tag == "shoesPlaced" || co.tag == "policePlaced" || co.tag == "firePlaced" || co.tag == "archPlaced" || co.tag == "archieoPlaced" || co.tag == "programmerPlaced")
        {
                panel.SetActive(false);
                text.gameObject.SetActive(false);
                gameObject.GetComponent<Pickupable>().enabled = false;
            
        }
    }

    void OnTriggerExit(Collider co)
    {
        if (co.tag == "shopPlaced" || co.tag == "schoolPlaced" || co.tag == "hospitalPlaced" || co.tag == "fitnessPlaced" || co.tag == "micPlaced" || co.tag == "shoesPlaced" || co.tag == "policePlaced" || co.tag == "firePlaced" || co.tag == "archPlaced" || co.tag == "archieoPlaced" || co.tag == "programmerPlaced")
        {
            panel.SetActive(false);
            text.gameObject.SetActive(false);
        }
    }

Hi;
in lane 25 u have a “else” function that set every thing to false if it collide some thing else ;
so i think this happense and make your objects dissactive;

Your problem is most likely in OnTriggerStay() since you are disabling the object when the two objects are still touching. It could also be related to OnTriggerExit() if the object leaves the collision unexpectedly, but as stated it’s most likely the former.

Note: This post was edited as I did some research and revised my solution

I’m not 100% certain as i’m fairly new to unity and C# myself, but I don’t believe that using OnTriggerStay() as an IEnumerator (line 35) would work properly like that, as it it is a method that has already been pre-defined elsewhere. If it works as I imagine it would, OnTriggerStay() would not run this line:

yield return new WaitForSeconds(5);

As the method isn’t inherently an IEnumerator, meaning it would move on to running the rest of the code immediately. This means if any of the ‘if’ statements that follow are true, the panel would be immediately disabled. The method will keep on running regardless of delays, even using a co-routine with this yield line included.

To get around this, i suggest calling a seperate Coroutine from within OnTriggerStay() to handle the entirety of your code instead of keeping it all in the Coroutine. Try try replacing OnTriggerStay() with something such as this:

void OnTriggerStay(Collider co)
{
     StartCoroutine(WaitForFive());
}

Then add a seperate method to perform the task for you:

IEnumerator WaitForFive()
{
    yield return new WaitForSeconds(5);

// Rest of the code you want to execute after the delay
}

I haven’t tested the code above directly, but hopefully it gives you the right idea to solve your problem. Good luck!