How to switch light off after game object has leaved another game object?

I have overall 3 game objects in my game. Two bars and one sphere. Bar 1 is at x=-6 and y=-2 and bar2 at x=6 and y=2; I would like to follow bar 1 and as soon as my sphere leaves bar 1 I would like to switch off the light for some seconds. So that the player has to move the sphere on bar 2 without seeing anything (based on experience on what he saw).

Is this possible? I thing with a collider function?

You can add any collider on an object and set “Is Trigger” to true. Then these functions will be called on any script attached to the same GameObject:

OnTriggerEnter

OnTriggerExit

OnTriggerStay

In these functions you can enable/disable the lights in your scene.

Example:

void OnTriggerEnter(Collider other) {
    light.enabled = true;
}

void OnTriggerExit(Collider other) {
    light.enabled = false;
}