- Home /
Flashlight timer
Hello,
I'm trying to create a script which has a simple timer in the background, and change the intensity of a light source (Spotlight attached to a torch mesh).
Not really sure how to reference the light source and change it's intensity. I've been looking at this for too long...
private var startTime;
private var restSeconds : int;
var Flashlight : Light;
var countDownSeconds : int;
function Awake()
{
startTime = Time.time;
var guiTime = Time.time - startTime;
restSeconds = countDownSeconds - (guiTime);
}
function update()
{
if (restSeconds == 60)
{
Flashlight.Light.intensity = 1;
Debug.logWarning("Light goes down");
}
}
Answer by iwaldrop · Apr 17, 2013 at 09:19 PM
Do you ever assign a value to Light? What's wrong with just using Flashlight.light?
Make sure that Flashlight inherits from Monobehaviour, that the GameObject that Flashlight is on has a Light Component attached to it, and call the following:
Flashlight.light.intensity = 1;
I have the script attached to the spotlight, and placed the 'Light component' into the free variable slot. Yet it still does nothing.
How would I set the intial value for the light intensity?
function Awake()
{
Flashlight.Light.intensity = 3;
}
Yet when I do this, I get a nullreference error. Not really sure how to proceed.
Thanks for the help.
It looks like you're intending to use Flashlight.intensity. But, again, you don't need a variable to store a reference to a Light, because every Component already contains a reference to the Light attached to the same GameObject. I misunderstood your code earlier, so light.intensity should get the job done too. :)
Remember that caps are important - it's just plain light.intensity
and also the intensity value is in the range of 0 to 1 where 0 is "off" and 1 is "full on".
For some special lights, and only if you're using HDR, a value greater than 1 might be useful, but I doubt for this.
Are you trying to make a timer and as the timer goes down the intensity goes down with it?
So, I added
Flashlight.light.intensity = 2;
On awake and that definately changes the intensity. I think I've broken the timer as I don't think it ever reaches 60 seconds.
As for the timer, yes. At a specific time, I want the intensity to decrease slightly.
Answer by Dracorat · Apr 17, 2013 at 11:17 PM
These two lines need to be in the beginning of the Update function, not the Awake function:
var guiTime = Time.time - startTime;
restSeconds = countDownSeconds - (guiTime);
Your answer
![](https://koobas.hobune.stream/wayback/20220613100519im_/https://answers.unity.com/themes/thub/images/avi.jpg)