How can I use a game object as an on/off switch?

I have the following code:

void OnTriggerStay() {
    if (paused == false) {
        paused = true;
        renderer.material.color = Color.green;
    } else {
        paused = false;
        renderer.material.color = Color.blue;
    }
}

What I would like to do is to have the trigger set pause to true and then next time to false.

This works but only sometimes. Usually when the trigger is fired the object color changes from one color to the other and then back almost straight away. Is there a way I could add some hysteresis to this so that it can’t immediately change back for a short time?

“OnTriggerStay is called almost all the frames for every Collider other that is touching the trigger.”

So it’s constantly switching between them.

I would use OnTriggerEnter and OnTriggerExit. In OnTriggerEnter toggle your pause and disable a boolean that indicates whether you have to toggle your pause and in your OnTriggerExit, enable that boolean so the next time the pause will get toggled.

I’m so new to Unity and I suggest this based on my previous experience, maybe Unity has some facility to make this easier. I just wanted to get you started.