- Home /
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?
Yeah with a collider would be best. $$anonymous$$y way of doing it probably isn't the best and maybe a lot of programmers out there might say that it is a bad way to do it but, on leave destroy the light and when (if) the player gets to the other bar instantiate a prefab of the light. Also, you could put it on a timer so that if the player doesn't get to the bar in time then they fail other wise they can just try for ever.
Answer by jebemti · Mar 17, 2016 at 10:49 PM
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:
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;
}