x


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!

Messjuh

more ▼

asked Jun 05 '12 at 05:23 PM

Messjuh gravatar image

Messjuh
0 1 1 1

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

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; }
}
more ▼

answered Jun 05 '12 at 06:23 PM

Loius gravatar image

Loius
10.9k 1 12 43

(comments are locked)
10|3000 characters needed characters left

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!

more ▼

answered Jun 09 '12 at 02:51 PM

Messjuh gravatar image

Messjuh
0 1 1 1

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x326
x241
x230
x123

asked: Jun 05 '12 at 05:23 PM

Seen: 506 times

Last Updated: Jun 09 '12 at 02:51 PM