system
1
I want to make my lights blink. I have a game object called Main Lights. I was thinking that a possible way to make the effect would be to destroy and then instantiate Main Lights on update, but I'm not really great with code, and I feel like there must be an easier or simpler way to create the effect. Any suggestions would be helpful. Thanks!
Eric5h5
2
Indeed, repeated destroying and instantiating is not a great idea since it uses resources unnecessarily.
var timer = 1.0;
function Start () {
while (true) {
yield WaitForSeconds(timer);
light.enabled = !light.enabled;
}
}
if you destroy and instantiate a gameObject, you destroy allot of resources. you can change the range property of a light or enable/disable it in shor intervals to make the effect. you should not do it in Update because it will become framerate dependent. you should use a coroutine.
vat waitingTime=0.05; //20 times a second
var normalRange=3; //normal range of light
var flRange=0.5; //flickering range
function Start()
{
while (true)
{
if (light.range == normalRange) light.range = flRange; else light.range = normalRange;
yield WaitForSeconds(waitingTime);
}
}
you can turn on or off enabled property too. i'll show that in C#
public float waitingTime=0.05;
IEnumerator Start ()
{
while (true)
{
light.enabled = !(light.enabled); //toggle on/off the enabled property
yield return new WaitForSeconds(waitingTime);
}
}
i did not test any of these values and you should edit them to get best timer values.
You may use this script 
using UnityEngine;
using System.Collections;
public class PoliceLight : MonoBehaviour {
public Light RedLight;
public Light BlueLight;
public int Number = 1;
// Use this for initialization
void Start ()
{
Number = 1;
BlueLight.light.intensity = 0;
RedLight.light.intensity = 0;
}
// Update is called once per frame
void Update ()
{
if (Number == 1)
{
BlueLight.light.intensity = 0;
RedLight.light.intensity = 1.5f;
StartCoroutine(waitforred());
}
if (Number == 2)
{
RedLight.light.intensity = 0;
BlueLight.light.intensity = 1.5f;
StartCoroutine(waitforblue());
}
}
IEnumerator waitforred()
{
yield return new WaitForSeconds (0.2f);
Number = 2;
}
IEnumerator waitforblue()
{
yield return new WaitForSeconds (0.2f);
Number = 1;
}
}
system
4
You can also just have two textures - one texture lighter than the other and then swap textures with a timer. This works well for things such as blinking warning lights that do not need to cast light.