- Home /
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
Answer by Loius · Jun 05, 2012 at 06:23 PM
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; }
}
Answer by Messjuh · Jun 09, 2012 at 02:51 PM
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!
Your answer
![](https://koobas.hobune.stream/wayback/20220613072408im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Gun Switch and SetActive is not Working! 2 Answers
How to make rendering quicker 2 Answers
Counting active objects in a list 3 Answers
Objects move on play 3 Answers
GameObject.Find() work on inactive objects 16 Answers