Fade Light in OnTriggerEnter and Fade Light out OnTriggerExit

Hey all,

Trying to get a light to fade in and out when the player enters and exits a boxcollider (is Trigger is selected). I took some stuff from here Unity Connect
but am getting a lot of flickering when I attach this script to the box collider gameObject. I can tell something is happening when I enter and exit the collider but looks terribly messed up. I can get the light to turn on and off with no problem but when I introduce the lerping and if statements I get thrown off. Any ideas? I imagine I’m doing something really dumb here but i’m but a lowly n00b

using UnityEngine;
using System.Collections;

public class fadelight : MonoBehaviour {

	public Light light;
	public float fadeSpeed = 2f;
	public float highIntensity = 6f;
	public float lowIntensity = 0.0f;
	public float changeMargin = 0.2f;
	public bool lightison = false;
	private float targetIntensity;

	void Awake(){
		light.enabled = false;
	}

	void OnTriggerEnter() {
		lightison = true;
	
	}

	void OnTriggerExit() {

		lightison = false;
	}

	void Update() {
		if (lightison == true) {
			light.enabled = true;
			light.intensity = Mathf.Lerp (lowIntensity, highIntensity, fadeSpeed * Time.deltaTime);
		} else {
			light.intensity = Mathf.Lerp (highIntensity, lowIntensity, fadeSpeed *Time.deltaTime);
		
		}
	}
}

Its because of the Lerp you used the intensity of the light is not reached the destination intensity(low/final). Intensity of light keeps varying. Clamp the value using condition to avoid flickering.

So @Umresh you’re saying that as long as I’m onTriggerEnter then it’s consistently going between low and high or vice versa and that’s why it’s flickering? will look at clamp haha thanks