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;
}
}
}
}
Answer by hexagonius · Aug 28, 2015 at 08:12 PM
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
And if it is all of it in the original post, you're missing a closing curly brace on the last line to close the Update function block
Actually the errors are on line 55, column 16; and line 65, column 10. They refer to not having closing brackets '}' but ins$$anonymous$$d have an 'else' statement. But a strange coincidence that there are errors on lines 10 and 16!
Answer by ransomink · Sep 06, 2015 at 09:51 PM
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
@Gigioparanormal: Were you able to test or check out the flashlight script?
Answer by ransomink · Aug 29, 2015 at 01:17 AM
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!
Answer by Gigioparanormal · Aug 29, 2015 at 11:11 AM
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'.
You should change your answer to a comment under my answer, so every time you respond it isn't as a new answer. (Will be confusing for other looking to help or find the same solution.)
I just looked at the code I put up and line 6 is: var accesa : boolean;
. There is no '\r' on that line. Can you put up the new code you are working with and we can go from there...
Sorry, I'm not good in Javascript, i created an another script but there are not an error, but is similar when i press F or another key, i can't turn on the light, why? There is another script
var linkedLight : Light; var switchon: AudioClip; var switchoff: AudioClip;
var is_on: boolean = false;
var maxPower : float = 100;
private var currentPower : float;
var speed : float = 5.0;
private var alpha : float;
private var duration : float = 0.2;
private var baseIntensity : float;
function Start(){
currentPower=maxPower; baseIntensity=linkedLight.GetComponent.().intensity; }
function Update () {
if(Input.GetButtonUp("Flash")){
if (is_on)
{
flash_off();
is_on=false;
}
else
{
flash_on();
is_on=true;
linkedLight.GetComponent.().intensity = baseIntensity; }
}
if(currentPower < maxPower/4 && linkedLight.enabled){ var phi : float = Time.time / duration 2 $$anonymous$$athf.PI; var amplitude : float = $$anonymous$$athf.Cos( phi ) * .5 + baseIntensity; linkedLight.GetComponent.().intensity = amplitude + Random.Range(0.1, 1.0) ; } linkedLight.GetComponent.().color = Color(alpha/maxPower, alpha/maxPower, alpha/maxPower, alpha/maxPower); alpha = currentPower;
if (is_on==true) {
if(currentPower > 0.0) currentPower -= Time.deltaTime * speed;
if(currentPower <= 0.0) {flash_off(); is_on=false;}
}
if (is_on==false) {
if(currentPower < maxPower) currentPower += Time.deltaTime * speed/2;
}
} function flash_on (){
Camera.main.GetComponent.().PlayOneShot(switchon);
linkedLight.enabled =true;
}
function flash_off(){ Camera.main.GetComponent.().PlayOneShot(switchoff);
linkedLight.enabled =false;
}
//If you want to display percentage of charge just uncomment all lines which under function OnGUI () { GUI.Label (Rect(70, Screen.height - 75,150,60), "Battery: " + currentPower.ToString("F0") + "%"); }
@Gigioparanormal Hey there, I'm currently working out your problem! I'll let you know when I'm done! :)
Answer by binaryspace1010 · Aug 31, 2015 at 09:21 PM
@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; }'
There are just four errors, and there is the errors
Assets/Scripts/Pick up a flashlight.js(59,1): BCE0044: expecting EOF, found '}'. Assets/Scripts/Pick up a flashlight.js(23,58): BCE0043: Unexpected token: (. Assets/Scripts/Pick up a flashlight.js(23,58): UCE0001: ';' expected. Insert a semicolon at the end. Assets/Scripts/Pick up a flashlight.js(23,57): BCE0043: Unexpected token: ..
I don't know where line 59 is or the other line errors because half the code is outside the code block. I'll go ahead and try to create a new script for turning on/off the flashlight myself...
Your answer
Follow this Question
Related Questions
How do I make a working flashlight with batteries? 0 Answers
Error : Unknown identifier : FlashLight 1 Answer
enemy patrolling using javascript??? 0 Answers
First Person Controller error, "IndexOutOfRangeException:Array index is out of range." 1 Answer
How to use an implicit generic list as a parameter? 1 Answer