- Home /
How to make a light turn on/off
Hi im very new to unity scripting and I was wondering how to make a light switch so when I go into the trigger and press 'e' it turns on and off the linked light here's what I tried:
var linkedLight : Light; function OnTriggerEnter(otherObj: Collider){
if(Input.GetKeyDown("e")){
linkedLight.enabled = !linkedLight.enabled;
} }
function LightOff (){ linkedLight.enabled = false;
}
If you know how to do this it would be a huge help. Thanks! :)
Answer by DocteurCox · Jun 26, 2013 at 08:07 PM
I am not sure to understand the question, but I assume you want the light to be switchable only when close to switch. Then you would need to place your code in OnTriggerStay instead of OnTriggerEnter.
Your answer
![](https://koobas.hobune.stream/wayback/20220613104931im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
c# light off 2 Answers
Light script flashes on and off 1 Answer
Turn On and Off Lights Via Script 1 Answer
Turn off a script and then back on? 3 Answers
How to turn on a light by pressing and after few seconds turn it off 1 Answer