On/Off switch for active objects...any ideas?

Hey Folks!

First of all, I am a greenhorn in coding and unity! So please excuse my newbie question!

Im working on a 2D gamescene, where a handful of possible actions should only become enabled (or unlocked)
when I activate another defined action in that scene first (kind of on/off switch, but without the “off” or
“back” function).

To be more precise, my scene is set in a room where, once you turn off the light, certain new animated active
objects become visible, or appear and can be activated by clicking them . Also some objects that are visible
and active from the beginning of the scene, should play different animations/sounds, once the light is turned off.

I need a script for the on/off switch, which manages the state of several objects being active(if on) or inactive (if off)

Somehow I couldnt find any help on my odyssey through the forums and the web yet…
Thanks a lot in advance for all your brilliant answers and solutions!

Best regards!


Use a script that has:

var affectedObjects : Controllable[];

When the switch is activated:

for each object : Controllable in affectedObjects {
  if ( on ) object.TurnOn();
  else object.TurnOff();

A Controllable would just be this:

class Controllable {
    function TurnOn() {}
    function TurnOff() {}

Your other classes would inherit from that:

class Lamp extends Controllable {
  var light : GameObject;
  function TurnOn() { light.active = true; }
  function TurnOff() { light.active = false; }

Thank you very much! I will try it as soon as Im back from my vacation! But it already sounds like this could be the right direction for solving my problem…thanks again!