FlashLight/pick up script support

Hello, Not sure if anyone can help me or not what i’m having problem is following:

FlashLight.js

#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.GetComponent.<Light>().intensity = 5;
    }
        if(lightOn && batteryLife <= 0.3)
    {
        flashlightLightSource.GetComponent.<Light>().intensity = 4;
    }
    if(lightOn && batteryLife <= 0.2)
    {
        flashlightLightSource.GetComponent.<Light>().intensity = 3;
    }
        if(lightOn && batteryLife <= 0.1)
    {
        flashlightLightSource.GetComponent.<Light>().intensity = 2;
    }
            if(lightOn && batteryLife <= 0)
    {
        flashlightLightSource.GetComponent.<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)
    {
        GetComponent.<AudioSource>().clip = soundTurnOn;
    }
    else
    {
        GetComponent.<AudioSource>().clip = soundTurnOff;
    }
    GetComponent.<AudioSource>().Play();
   
}

    @script RequireComponent(AudioSource)

That the flashlight script

This is the battery.js

var buttonInRange;
var buttonActivated;
var batterySound : AudioClip;
private static var batteryPower : float = 10;

public var guiSkin : GUISkin;

private var hasPlayed = false;

function OnTriggerEnter (c : Collider)
{
    buttonInRange = true;

}
function OnTriggerExit (c : Collider)
{
    buttonInRange = false;

}
function OnGUI ()
{
    if(buttonInRange == true)
    {
        GUI.skin = guiSkin;
        GUI.Label (Rect (Screen.width/2-50, Screen.height/2-55, 120, 50), "Pick up batteries");
   
    }

}
function Update ()
{
    if (buttonInRange == true)
    {
        if (Input.GetKeyDown ("e"))
        {
            if(!hasPlayed)
            {
                AudioSource.PlayClipAtPoint(batterySound, transform.position);
                FlashLight.AlterEnergy(batteryPower);
                Destroy(gameObject);
                hasPlayed = true;
           
            }
       
        }
   
    }

}

Problem i’m having with this is when pick up “item” The values go up correct but light doesn’t turn on till half empty, Been looking into it but can’t find the problem at all, Does anyone see the error making this happen?

Thank you

first problem is that your “AlterEnergy” function doesn’t use it’s parameter, it’s using the local variable in the flashlight script.

secondly you’re setting the “barDisplay” before zeroing the batteryLife below zero, so in some cases the fill amount will be scaling the wrong way.

lastly, once the battery has been emptied there is no line of code to tell the intensity to change until it’s below 0.4. i.e. line 63 is called, then batterylife is 1, then follow through the logic again, no intensity changes.

lastly, why are you using the old clunky OnGUI? it was replaced for in game UI in unity 4.6.