Need help with Flashlight Script

I am getting close to done with a game that I have been making for awhile now. It’s a maze type/horror-ish game where you find clues to escape the maze, and there’s an enemy that follows you, but can’t harm you… Just follows and annoys you. Here’s my question, I want the enemy to make the player’s flashlight flicker randomly once he’s within a certain distance of you (this adds to the annoyance factor). I tried a few scripts myself, but it failed. I hope this isn’t too much to ask, Thanks!

Using Random.Range(float, float), and adjusting the intensity of the flash light you can make the flash light flicker. to make this happen is to get the enemies position, and you position and use Vector.Distance. its a very simple one,

private var close2Enemy : boolean;
var flashligh : Light;
var enemy : GameObject
var range
var flickerIntesity

function Update() {
if(close2enemy) {

function CheckDistance() {
var distance Vector.Distance(transform.position, enemy.transform.position)
if (distance <= range) {
  close2enemy = true;
else {  
 close2enemy = false;
function ApplyFlicker() {
   flashlight.intensity += Random.Range(-flickerIntensity,flickerIntensity) 


I wrote this out quickly and its real messy, i suggest you recreate something more to your liking, instead of Random.Range, you can use Math.Tan and multiply that with intensity
you may want to use a co-routine for the actually flickering;
there’s a post you might want to have a look at here:

and here is some docs for you to read: