Script error to pick-up flashlight

Hi, sorry for my english but I’m italian. Ehm i created a script for pick-up a flashlight but there is 3 errors (some words are italian but you can understand)

(there are 2 errors of) Assets/Torcia.js(55,16): BCE0044: expecting }, found ‘else’.
(and 1 of) Assets/Torcia.js(65,10): BCE0044: expecting EOF, found ‘}’.

what i can do?

This is the script

var raccolta : boolean;
 var posizioneT : Transform;
 var maxDist : float;
 var maxAngolo : float;
 var mainCamera : Transform;
 var accesa : boolean;
 var luce : Light;
 var moltiplucatore : float;

 function update ()
 {
 if(!raccolta) 
{
 GetComponent(Collider).enabled = true;
 rigidbody.useGravity = true;
 rigidbody.iaKinematic = false;
 transform.parent = null;

 if(Input.GetButtonDown("Fire1") && Vector3.Distance(transform.position, mainCamera.position) < maxDist && Vector3.Angle(transform.position - mainCamera.position, mainCamera.forward) < maxAngolo);
 {
 raccolta = true;
 }
 else
 {
 GetComponent(Collider) = false;
 rigidbody.useGravity = false;
 rigidbody.iaKinematic = true;
 transform.parent = mainCamera;
 transform.position = posizioneT.position;
 trasform.rotation = posizioneT.rotation;

 if(accesa)
 {
 luce.intensity -= Time.deltaTIme * 0.1 * moltiplicatore;
 luce.enabled = true;
 if(Input.GetButtonDown("Fire2")) accesa = false;
 if(Input.GetButton("Fire3"))
 {
 luce.range = 15;
 luce.spotAngle = 12;
 }
 else
 {
 luce.range = 10;
 luce.spotAngle = 30;
 }
 else
 {
 luce.enabled = false;
 if(Input.GetButtonDown("Fire2")) accesa = true;
 }
 }
 }
 }

both errors tell you which lines they appear (10 and 16). if you look at 10, update needs to be uppercase U, on 16, it’s isKinematic, not iaKinematic. If using Monodevelop a doubleclick on an error within unity should take you exactly there

I went ahead and recreated the script for turning the flashlight on/off. I simplified draining the battery and charging the battery by placing them inside of functions. It is much easier than having long if statements inside of Update(). I moved the flicker code within a function as well, so if you ever need to access these again, you can simply call the function name.

Everything important is commented (well, I hope) and the Debug.Log() are there for testing purposes; you can delete or comment those out.

In the start function—at the bottom of the script—I gave you two options for referencing the flashlight. The comments explain their use but both of them are being executed, so choose which one you wish to use and comment out the other.

I’ve tested it myself and it works wonders. Please let me know if any problems persist. I hope this helps!

Character limit exceeded, so I’ll post a pastebin file: Flashlight

Your code is quite confusing and not as readable. This problem stems from its format/layout. You should definitely indent your curly brackets to better understand what piece of code lies in each scope of the project.

Line 10: Needs to be Update (capital “U”)

function update ()

Line 16 Needs to be rigidbody.isKinematic

rigidbody.iaKinematic = false;

Line 25: Needs to be GetComponent(Collider).enabled = false;

GetComponent(Collider) = false;

Line 27: Needs to be rigidbody.isKinematic

rigidbody.iaKinematic = true;

Line 30: Needs to be transform.rotation

trasform.rotation = posizioneT.rotation;

These were the ones I could find; hopefully there isn’t more. This next part is difficult because I can’t tell if some of the if statements are within the same scope or on their own (if they’re inside of another if statement or by itself.

The if statement @ Line 37 has 2 else statements which is not possible. It can be:

// first option
if ( option1 )
{
	// do something
}
// second option
else if ( option2 )
{
	// do something
}
// neither option
else
{
	// do something
}

or:

// first option
if ( option1 )
{
	// do something
}
// second option
else ( option2 )
{
	// do something
}

NOTE: You should really indent for class, function, enum, if, for, etc.

The if statement on line 19 encapsulates the rest of the script. Is this suppose to happen or should there be a closing bracket on line 31?

// line 19 below
if(Input.GetButtonDown("Fire1") && Vector3.Distance(transform.position, mainCamera.position) < maxDist && Vector3.Angle(transform.position - mainCamera.position, mainCamera.forward) < maxAngolo);
{
raccolta = true;
}
else
{
GetComponent(Collider) = false;
rigidbody.useGravity = false;
rigidbody.iaKinematic = true;
transform.parent = mainCamera;
transform.position = posizioneT.position;
trasform.rotation = posizioneT.rotation;
// line 31

I attempted to format your code, but as I previously stated, I don’t know where your closing brackets should be, so it may be incorrect. After looking at the new format, you can easily fix the misplaced brackets.

After looking more at the code, the if statements look out-of-order. You have 2 if statements with the same condition setting the same variable to different values. I’m not sure what it suppose to happen, but here is the formatting at least…

var raccolta        : boolean;
var posizioneT      : Transform;
var maxDist         : float;
var maxAngolo       : float;
var mainCamera      : Transform;
var accesa          : boolean;
var luce            : Light;
var moltiplucatore  : float;

function Update ()
{
	if (!raccolta) 
	{
		GetComponent(Collider).enabled = true;
		rigidbody.useGravity           = true;
		rigidbody.isKinematic          = false;
		transform.parent               = null;
		
		if (Input.GetButtonDown("Fire1") && Vector3.Distance(transform.position, mainCamera.position) < maxDist && Vector3.Angle(transform.position - mainCamera.position, mainCamera.forward) < maxAngolo);
		{
			raccolta = true;
		}
		else
		{
			GetComponent(Collider) = false;
			rigidbody.useGravity   = false;
			rigidbody.isKinematic  = true;
			transform.parent       = mainCamera;
			transform.position     = posizioneT.position;
			transform.rotation     = posizioneT.rotation;
		}
		
		if (accesa)
		{
			luce.intensity -= Time.deltaTIme * 0.1 * moltiplicatore;
			luce.enabled    = true;
		}
		
		if (Input.GetButtonDown("Fire2")) accesa = false;
		
		if (Input.GetButton("Fire3"))
		{
			luce.range     = 15;
			luce.spotAngle = 12;
		}
		else
		{
			luce.range     = 10;
			luce.spotAngle = 30;
		}
			
		luce.enabled = false;
		
		if (Input.GetButtonDown("Fire2")) accesa = true;
	}
}

Hope this helps!

Sorry, I tried to solve the errors and write your script, in your script there are only one error

Assets/Pick-up flashlight.js(6,32): BCE0044: expecting ‘’', found ‘\r’.

@Gigioparanormal It’s not Perfect, but i have made a piece of code that works! Here it is:

'#pragma strict

var flashLight : Light;

var switchon: AudioClip;

var switchoff: AudioClip;

var is_on: boolean = false;

var maxPower : float = 100;

var currentPower : float;

var speed : float = 5.0;

private var baseIntensity : float;

function Start()
{

currentPower = maxPower;

flashLight = GameObject.Find(“Point light”).GetComponent.();

baseIntensity = flashLight.intensity;
}

function Update ()
{

if(Input.GetKey(“f”)) // If the “F” key is pressed
{

if (is_on == false)
{
FlashOn();
}

else if(is_on == true) {
FlashOff();
}
}

if (is_on == true) 
{  
	if(currentPower > 0.0) 
            {
                    currentPower -= Time.deltaTime * speed; 
            }

            if(currentPower <= 0.0) 
	{
		FlashOff(); 
		is_on = false;
	} 
}

if (is_on == false) 
{    
	if(currentPower < maxPower) currentPower += Time.deltaTime * speed/2; 
}

if(currentPower < maxPower / 4 && is_on == true)
{
	//Flicker Light
	
}

}

function FlashOn ()
{
flashLight.enabled = true;
is_on = true;
}

function FlashOff()
{
flashLight.enabled = false;
is_on = false;
}’