Hello guys!I’m making a horror game and i’m messing with a realistic flashlight.I tried to find a good flashlight + battery life indicator,i searched for an hour and the most functional was from this vid:
And i inserted the code and followed EVERYTHING,here is it:
#pragma strict
var flashlightLightSource : Light;
var lightOn : boolean = true;
var lightDrain : float = 0.1;
private static var batteryLife : float = 0.0;
var maxBatteryLife : float = 2.0;
private static var batteryPower : float = 1;
var barDisplay : float = 0;
var pos : Vector2 = new Vector2(20,40);
var size : Vector2 = new Vector2(60,20);
var progressBarEmpty : Texture2D;
var progressBarFull : Texture2D;
var soundTurnOn : AudioClip;
var soundTurnOff : AudioClip;
function Start()
{
batteryLife = maxBatteryLife;
flashlightLightSource = GetComponent(Light);
}
static function AlterEnergy (amount : int)
{
batteryLife = Mathf.Clamp(batteryLife+batteryPower, 0, 100);
}
function Update()
{
//BATTERY LIFE BRIGHTNESS//////////
if(lightOn && batteryLife >= 0)
{
batteryLife -= Time.deltaTime * lightDrain;
}
if(lightOn && batteryLife <= 0.4)
{
flashlightLightSource.light.intensity = 5;
}
if(lightOn && batteryLife <= 0.3)
{
flashlightLightSource.light.intensity = 4;
}
if(lightOn && batteryLife <= 0.2)
{
flashlightLightSource.light.intensity = 3;
}
if(lightOn && batteryLife <= 0.1)
{
flashlightLightSource.light.intensity = 2;
}
if(lightOn && batteryLife <= 0)
{
flashlightLightSource.light.intensity = 0;
}
barDisplay = batteryLife;
if(batteryLife <= 0)
{
batteryLife = 0;
lightOn = false;
}
if(Input.GetKeyUp(KeyCode.F))
{
toggleFlashlight();
toggleFlashlightSFX();
if(lightOn)
{
lightOn = false;
}
else if (!lightOn && batteryLife >= 0)
{
lightOn = true;
}
}
}
/////// PIC ///////////
function OnGUI()
{
// draw the background:
GUI.BeginGroup (new Rect (pos.x, pos.y, size.x, size.y));
GUI.Box (Rect (0,0, size.x, size.y),progressBarEmpty);
// draw the filled-in part:
GUI.BeginGroup (new Rect (0, 0, size.x * barDisplay, size.y));
GUI.Box (Rect (0,0, size.x, size.y),progressBarFull);
GUI.EndGroup ();
GUI.EndGroup ();
}
function toggleFlashlight()
{
if(lightOn)
{
flashlightLightSource.enabled = false;
}
else
{
flashlightLightSource.enabled = true;
}
}
function toggleFlashlightSFX()
{
if(flashlightLightSource.enabled)
{
audio.clip = soundTurnOn;
}
else
{
audio.clip = soundTurnOff;
}
audio.Play();
}
@script RequireComponent(AudioSource)
////////You are welcome/////////
But for some reason,when i turn off the flashlight,to preserve my battery,the error was that when i turned my flashlight off unity crashed,but i just changed the flashlight location from main camera to firstpersoncontroller.But now,it does turn off,and work properly except that THIS happens in my console:
MissingComponentException: There is no ‘Light’ attached to the “First Person Controller” game object, but a script is trying to access it.
You probably need to add a Light to the game object “First Person Controller”. Or your script needs to check if the component is attached before using it.
Flashy.toggleFlashlight () (at Assets/Standard Assets/Flashy.js:112)
Flashy.Update () (at Assets/Standard Assets/Flashy.js:77)
Anyone know the issue?,I’m using unity 4.6.2 because unity 5 glitched the render setting so i cannot make it night or foggy,which is the version ryandome used,and also,my flashlight keeps on after the error,yet it still displays the code.