- Home /
Turn all game lights off after a collision
I would like to find out how i can have all the lights in my game be turned of so the scene becomes pretty much pitch black. So when one of my characters collides with another object i want to have he lights intensity turn down. I am very very new to unity so it would help if you also stated what i would attach the script to and stuff like that. Thanks
Answer by Hexer · Jan 29, 2015 at 09:42 PM
I would use,
FindGameObject("NameOFgameObj_with/Lightsource").GetComponent(Light).enabled=false;
but when you have to many lightsources in your scene this can be a little bit messy. In that case you want to make some kind of FADE IN effect. There are plenty of tutorials on the internet.
...Or add some kind of transparant/black layer.
Okay thanks for the reply, I do have 3 lights in the scene but I will try and use what you suggested, and this may sound stupid, but do I put that in the void start and add put it in a if statement? So if there is a collision then the lights fade out and what would I attach this too? Thanks so much for you answer
1 : You add void OnTriggerEnter() and put the disable lightsource code in an if-statement.(if you use the script as seen below, make sure to add the tag (Player) to the player. (or change it to any desired tag)
void OnTriggerEnter(Collider other)
if(other.gameObject.tag == "Player"){
FindGameObject("NameOFgameObj_with/Lightsource").GetComponent(Light).enabled=false;
}
2 : You attach your script to the object with the collider. (btw if you are using a solid object like a cube, you want to make sure the collider is a little bit bigger than the object)
here is a link to Unity references I use it all the time when trying to make a script.: http://docs.unity3d.com/ScriptReference/Collider.OnTriggerEnter.html
http://docs.unity3d.com/ScriptReference/GameObject-tag.html The first link tells you more about the use of OnTriggerEnter and the second link is a reference to gameObject.tag I also suggest to watch a video about using tags.
Your answer
![](https://koobas.hobune.stream/wayback/20220613181913im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Why do 2D Lights render past shadowcasters. 1 Answer
Some lights aren't working. 0 Answers
Trying to make light pass through object 0 Answers
Why my 2D shadows have low resolution? 0 Answers