Disable boolean when triggerEnter and Enable when triggerExit

Hello.
I have a little problem for enable/disable a boolean with OnTriggerEnter and OnTriggerExit.

I wish to deactivate if the car touches the road and activate if it no longer touches the road.

I tested different method like the coRoutine or change OnTriggerEnter by OnTriggerStay but nothing changes.

I have the boolean which activates most often, even when the car touches the road.

Here is the script :

    private void OnTriggerEnter(Collider other)
    {
       if (other.name == GameObject.Find("Road").name && currentlyCarChoiceDump == GameObject.Find("Car1(Clone)"))
        {
            //StartCoroutine(stopRespawnButtonDelay(0));
            ScriptManager.GetComponent<Global>().stopRespawnButton[0] = false;
        }

        if (other.name == GameObject.Find("Road").name && currentlyCarChoiceDump == GameObject.Find("Car2(Clone)"))
        {
            //StartCoroutine(stopRespawnButtonDelay(1));
            ScriptManager.GetComponent<Global>().stopRespawnButton[1] = false;
        }

    }

    private void OnTriggerExit(Collider other)
    {
        //StartCoroutine(StopRespawnButtonDelay(other));

        if (other.name == GameObject.Find("Road").name && currentlyCarChoiceDump == GameObject.Find("Car1(Clone)"))
        {
                ScriptManager.GetComponent<Global>().stopRespawnButton[0] = true;
                //StartCoroutine(stopRespawnButtonDelay(0));
        }

        if (other.name == GameObject.Find("Road").name && currentlyCarChoiceDump == GameObject.Find("Car2(Clone)"))
        {
            //StartCoroutine(stopRespawnButtonDelay(1));
            ScriptManager.GetComponent<Global>().stopRespawnButton[1] = true;
        }
     
    }

Sorry for my bad english.
Thank you in advance for your help.

I think you would be better off creating a tag and having a “Player” tag and “Road” tag, currently you’re searching (looping) through every game object and finding “Road” which is CPU intensive and same with the others.

Then once you create the tags and tag the game objects you can use something like

if (other.gameObject.tag == “Road” )
{

}
or “Player” depending on where you put the script.

I saw the same thing as js3263854, you’re doing a gameobject.find. this is not correct.

i’ve updated to the code, and added some Debug.log() to verify that the you’re using the Ontrigger.
Do you have the colliders set to be a trigger? if you don’t get any debugs then you don’t have the colliders setup or you don’t have a rigid body

    private void OnTriggerEnter(Collider other)
    {
    Debug.Log("OnTriggerEnter : " + other.name);
       if (other.name == "Road" && currentlyCarChoiceDump == "Car1(Clone)")
        {
            Debug.Log("road and Car1");
            //StartCoroutine(stopRespawnButtonDelay(0));
            ScriptManager.GetComponent<Global>().stopRespawnButton[0] = false;
        }
        if (other.name == "Road" && currentlyCarChoiceDump == "Car2(Clone)")
        {
            Debug.Log("road and Car2");
            //StartCoroutine(stopRespawnButtonDelay(1));
            ScriptManager.GetComponent<Global>().stopRespawnButton[1] = false;
        }
    }
    private void OnTriggerExit(Collider other)
    {
    Debug.Log("OnTriggerExit : " + other.name);
        //StartCoroutine(StopRespawnButtonDelay(other));
        if (other.name == "Road" && currentlyCarChoiceDump == "Car1(Clone)")
        {
            Debug.Log("road and Car1");
            ScriptManager.GetComponent<Global>().stopRespawnButton[0] = true;
            //StartCoroutine(stopRespawnButtonDelay(0));
        }
        if (other.name == "Road" && currentlyCarChoiceDump == "Car2(Clone)")
        {
            Debug.Log("road and Car2");
            //StartCoroutine(stopRespawnButtonDelay(1));
            ScriptManager.GetComponent<Global>().stopRespawnButton[1] = true;
        }
    
    }

Hello,
@johne5 , I always get the messages as well, I did the test a little earlier.
The bool activates and deactivates randomly.

@js3263854 , this problem of randomness can indeed come from a CPU overhead.
I’ll do the test and I’ll keep you posted.

Edit: Always the same problem.
The bool is always frightening.

I made a small video to show the problem :

it looks like the bool is working as expected. when the car leaves the ground it changes, when the car hits the ground it changes.
the only thing you could do is to have a bigger trigger collider.

It’s not working exactly. Looks on at some times when the car is on the road…Any chance you have more than 1 object named ground and/or more than 1 collider (trigger) receiving these messages?
You could keep count of the number of triggers you’re inside and use that integer for your calculations (or while counting, if it equals > 0 or 0 that you’re inside, set the bool then).

Problem solved.
I simply lower the box collider of the car.

Thank you for everything

Cool :slight_smile: Glad ya got it solved.