Flashlight battery help!!!

Hey guys I am completely new to scripting and I am currently trying to learn c# and java through YouTube

I’m trying to make a flashlight and battery system using this tutorial I found online

After the battery has completely runs out, I pick up a new battery but when I try to turn the flashlight bak on, it won’t turn on! The intensity of the light stays at 0!
It’s getting frustrating and I have tried the different scripts in the comments but they don’t work! Can anyone edit the script for me so the flashlight will turn back on?

I have attached the 2 java scripts

any help would be greatly appreciated!

2631877–185013–FlashLight.js (2.46 KB)
2631877–185014–Battery.js (791 Bytes)

hey, generally it’s better to just paste the code into the forums using the tags, saves people having to download the file, open the file, etc. before they can help.

//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)
// Battery.js
#pragma strict
var buttonInRange;
var buttonActivated;
var batterySound : AudioClip;
private static var batteryPower : float = 5;

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-60, Screen.height/2-55, 120, 50), "(E) Pick Up Battery");
 
    }

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

}

also if you’re new to all this you’re probably better off working with the new UI rather than learning the legacy system:

OnGUI was “replaced” by the canvas system in Unity4.6+, there are lots of older tutorials which won’t cover it, something to be aware of.

okay got it thank you
I will keep that in mind for next time

and with a rather disturbing sense of dejavu:

looks like they had similar problems from that tutorial too :smile:

@LeftyRighty haha that’s crazy XD would you know how to fix it???