- Home /
Change Light Color
Hello everyone..
I want to change my light color like blinking effect.. currently by default i have set green color of light.. but when my level starts i want to change my light color at random time in repetitive manner..
example: first time this effect starts after 5 seconds and thet it will start after 10 seconds and in that i want to change light color like green to white for 2-3 times.
So it will be like blinking effect or thunder effect.
But how will i do that? i know how to change the color of light.
I have referred Unity3d documentation.
Code i have written is here :
#pragma strict
private var light : GameObject;
private var done : boolean = false;
function Start () {
if(!IsInvoking("ThunderEffect"))
InvokeRepeating("ThunderEffect",0.01f,5.0f);
}
function Update () {
}
function ThunderEffect()
{
//GameObject.Find("Directional Light");
for(var i : int = 0; i < 6 ; i++)
{
if(done)
{
GameObject.Find("Directional light").light.color = EditorGUIUtility.HSVToRGB(182,255,188);
yield WaitForSeconds(1.00f);
done = false;
}
else
{
GameObject.Find("Directional light").light.color = Color.white;
yield WaitForSeconds(1.00f);
done = true;
}
}
}
But this code is not working and even not giving error.
Please help me guys.. Thanks in advance for your support and help..
You can try animate it ,or use the Random.range to make it blink when a certain value is available.
Answer by clunk47 · Oct 15, 2013 at 04:47 PM
Try something like this. Assign your color array in the inspector for this example.
#pragma strict
@script RequireComponent(Light);
var colors : Color[];
var onTime : float;
var offTime : float;
function Start()
{
DoFlicker();
ChangeTime();
}
function DoFlicker()
{
while(true)
{
yield WaitForSeconds(offTime);
light.enabled = true;
yield WaitForSeconds(onTime);
light.color = colors[Random.Range(0, colors.Length)];
light.enabled = false;
yield WaitForEndOfFrame();
}
}
function ChangeTime()
{
while(true)
{
yield WaitForSeconds(onTime);
onTime = Random.Range(0.01f, 0.25f);
offTime = Random.Range(0.025f, 0.5f);
yield WaitForEndOfFrame();
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613115947im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Toggle built in lighting in script 1 Answer
A node in a childnode? 1 Answer
Can't get dynamic shadows working 1 Answer
"Bounces" tick box is greyed out in Lightmapping window? 0 Answers
Lighting Issues with Unity. 2 Answers