- Home /
Blinking Lights
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!
Answer by Eric5h5 · Feb 15, 2010 at 02:20 AM
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; } }
Hi Eric,
Isn't it any problem (resources) putting a infinite loop in the start() function?
@Uzquiano: No, there's nothing inherently wrong with an infinite loop.
Answer by Ashkan_gc · Feb 15, 2010 at 05:17 AM
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.
Answer by Nomibuilder · Feb 12, 2015 at 12:58 PM
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;
         }
     }
     
Answer by bradjensen68 · Feb 16, 2010 at 06:41 PM
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.
Your answer
 
 
              koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                