- Home /
how do i make lights flicker
How can I make 5 point lights flicker on and off so only 1 is on at a time I’m not very good at coding so the easier to understand the better but anything would help.
Thank you all :)
What do you mean by flicker? Randomly go on and off or some pattern? Is there a time duration? Should they stay the same brightness or change intensity?
make a script that holds the GameObject of each light in a public variable. Then from within the script, access the variable GameObject's Light component, and set to enabled = true/false;
http://unity3d.com/support/documentation/ScriptReference/Light.html
http://unity3d.com/support/documentation/ScriptReference/Behaviour-enabled.html?from=Light
Answer by alpaca of zion · May 15, 2012 at 08:44 AM
// Make a list of all of your lights. You can add these in the designer.
public List<Light> lights = new List<Light>();
void Update()
{
// Run this routine every 100 frames or so. Random.value gives a number between 0 and 1.
if (UnityEngine.Random.value > .99f)
{
// Turn all of the lights off.
foreach (Light light in lights)
light.enabled = false;
// Turn on one light at random. Random.Range returns a float greater than or equal to 0
// and less than the count of lights.
lights[UnityEngine.Random.Range(0, lights.Count)].enabled = true;
}
}
I had a similar solution at first using an empty game object and the lights as children then var lights:Component[] and getting the light components of each.
Weirdly I could put them on and off in a foreach loop but would get index out of range if trying to access one particular. Even though I could see them in the inspector. So I went for an easier.
@alpacaofzion $$anonymous$$akes me wonder though, how are you tally-ing the frames? Do you have a private var counting each frame til the desired frame count is reached? I do like this solution..
Because this runs in the update function it is called once per frame. It changes the light about every 100 frames, not exactly. The trick is in "if (UnityEngine.Random.value > .99f)" If you wanted to change it 1 in every 5 frames you could use: "if (UnityEngine.Random.value > .80f)". The .80 is because you would be leaving it the same 4/5ths or 80% of the time.
Answer by Berenger · May 15, 2012 at 05:00 AM
If you want to toggle the lights one after the other at a regular interval, you'll need 1) a function to turn 1 light on and the other off. 2) A loop going through those light with a pause after each iteration. See coroutines.
Your answer
Follow this Question
Related Questions
Directional light disabling when player is looking away 1 Answer
How to make a GameObject not hittable by shadows ? 2 Answers
Do I still need lightmaps? 1 Answer
How to prevent Overlapping Lights from combining / adding intensity? 3 Answers
[2D] Is it possible to create a collider for 2D lights? 0 Answers