Flashing Lens Flare

Hi there, how would I make it so a Lens Flare’s Brightness flashes every 0.4 of a second? I also wanted it so I can turn it on and off again using F3. I’ve worked out how to do it with a light, but not flare!

Here is the script I did for a light:

private var on = true;
var timer =1;
 
function Start () {
    while (true) {
    	yield WaitForSeconds(0.4);
    	light.enabled = !light.enabled;
    }
}
function Update(){

if(Input.GetKeyDown(KeyCode.F3)){
if(on){
light.intensity =0;
on = false;
}
else {
light.intensity =1.0;
on = true;
}
}
}

So how can I change this into a lens Flare’s Brightness :slight_smile: Thanks!

If you wanted to turn off the entire object rather then adjusting the lens flare, and the light - you could do something along these lines.

//turn off by disabling
this.gameObject.SetActive(false);
//turn on by disabling
this.gameObject.SetActive(true);

I’ll try to give you a more detailed example of how a simple timed light comes on and off (perhaps a police car or something of that nature)

//keep track of the time wanted to change the light
private float timer;
//establish a boolto determine if the light needs to be changed
private bool lightSwitch;
//establish the current condition of the light (do I need to turn it on?  Do I need to turn it off?
private bool lightStatus;

void Start()
{
//Set the start time for how we want the light to become disabled
timer = 0.8f;
lightSwitch = false;
lightStatus = false;
}

void Update()
{
//make a keypress or some type of toggle, however you want to handle the light and set a condition to run the timer.
if (lightSwitch)
		{
			//Reduce time
			if(Timer > 0)
				Timer -= Time.deltaTime;
		//Check if time is lower then 0, if it is, make it 0.
			if(Timer < 0)
				Timer = 0;
		//Once it is 0 - do something
			if(Timer == 0)
			{
				//could change the color, reset the timer, call a method that handles resetting the time...
			}

		}
}

Here is what I’d do with your code, and things you need to consider.

//Start is only going to get ran one time at the start of the object.  Having a while true in there doesn't really do much in my opinion.
    
        function Update(){
         
        if(Input.GetKeyDown(KeyCode.F1)){
        if(on){
            //START YOUR COUNT DOWN TIMER
             this.gameObject.SetActive = false;
        }
        else if {
           //TURN off gameObject, set on to false, reset timer to 0.4 if it wasn't for next use!
             this.gameObject.SetActive = true;
        }
      }
    }

So by using the simple timer method I show’d you in my example, and making a method that would check “Are my lights on? - Oh they are, lets make them off.” - “Oh hes calling this method again, Are my lights on? - Oh they ain’t, lets make them on.” and at the end of the method you could reset timer to 0.8f or 0.4f or whatever you want.

Lemme know if you need more help.