- Home /
The question is answered, right answer was accepted
BCE0043 error, but dont know why
So I'm trying to write a turret script where the turret has a safe range, but can shoot at "target" if its closer than the safe range. Problem I'm having is making the variable for "see how close the target actually is"
My error code is: (9,1): BCE0043: Unexpected token: var.
Here is my script.
var findTarget : Transform ; // variable target
var lookAtDistance = 20; // variable how far turret can see you
var safeDistance = 15; // variable how far before you are safe from turret
var fireDelay : float;
function Update()
// variable how close target is
var distanceToPlayer = Vector3.Distance(this.transform.position, target.transform.position);
//look at target
transform.LookAt(target);
// make sure player is close enough then fire
if(distanceToPlayer <= safeDistance) {
fire();
}
Hey thanks Chronos for responding so quickly, The issue now is that its not identifying "target" in line 8 and 10 and doesn't identify "fire" in line 13
Not identifying means it is not initialized or coded. Are you sure that the Transform-variable-you-use-as-target are named either target
or findTarget
?
Do you know that fire()
and Fire()
are not the same?
Answer by Chronos-L · Apr 07, 2013 at 02:18 AM
When it says (9,1): BCE0043: Unexpected token: var.
, it means that the compiler is not expecting the word var
, so there is something right before the var
the compiler is expecting but that something is not there.
Correction
var findTarget : Transform ;
var lookAtDistance = 20;
var safeDistance = 15;
var fireDelay : float;
function Update(){ // <- You missed the {
var distanceToPlayer = Vector3.Distance(this.transform.position, target.transform.position);
transform.LookAt(target);
if(distanceToPlayer <= safeDistance) {
fire();
}
} //<- You might have missed this one as well
var findTarget : Transform ; // <- use the same variable
var lookAtDistance = 20;
var safeDistance = 15;
var fireDelay : float;
function Update(){
var distanceToPlayer = Vector3.Distance(this.transform.position, findTarget .transform.position);
transform.LookAt(findTarget);
if(distanceToPlayer <= safeDistance) {
Fire();
}
}
//This is missing
function Fire() {
}
Awesome it works, so now if i want it to fire my particle system i setup I would just add under Fire? like this?
function Fire() {
var "prefab"
}
No, you have to put the prefab variable outside the Fire()
function. I supposed that you are new to program$$anonymous$$g, so I am going to give you a quick crash course (read very careful, each word in there are meaningful):
var publicStuff : AnyValidType; //public variables that will show up in inspector
private var theScriptSecret : AnyValidType; //private variables that is accessible to the script itself
//A very simple function
function AnyFunction() {
var inFunctionVariable; //The variables that is created inside this function
...
}
//$$anonymous$$ost function will look like this, maybe with more input/parameters
function AFunctionThatProcessExternalData( inputFromOutside : AnyValidType ) : TheDataTypeOfOutput {
//Do some processing on the input
...
return outputOfTheProcessingToOutside;
}
Because you will need to assign the prefab from the inspector, you must make the particle-system-prefab-variable a public variable (there are other means, but this is the one we will focus on).
Your script should look like:
var muzzleFlash : ParticleSystem;
...
function Update() {
...
}
function Fire() {
//Instantiate the muzzleFlash
}
yea I'm new to scripting, thanks for helping me so far :P
So when I clone instantiate, nothing fires...
var findTarget : Transform ;
var lookAtDistance = 20;
var safeDistance = 15;
var fireDelay : float;
var prefab : ParticleSystem;
function Update(){
var distanceToPlayer = Vector3.Distance(this.transform.position, findTarget .transform.position);
transform.LookAt(findTarget);
if(distanceToPlayer <= safeDistance)
Fire(); }
function Fire() {
var clone : ParticleSystem;
clone = Instantiate(prefab, transform.position, transform.rotation);
}
Im trying to fire my "turretlaser" at the player, its just a ParticleSystem with one particle flying forward in z space. however it doesn't even show up when i press play.
Also how do I classify this as "Answered" like Elite$$anonymous$$ossy suggested?
I have reorganized your script a bit:
var findTarget : Transform ; var lookAtDistance = 20; var safeDistance = 15; var fireDelay : float; var prefab : ParticleSystem;
function Update(){
var distanceToPlayer = Vector3.Distance(this.transform.position, findTarget .transform.position);
transform.LookAt(findTarget);
//if(distanceToPlayer <= safeDistance)
//$$anonymous$$anually fire the particle for testing purposes
if( Input.Get$$anonymous$$eyDown( $$anonymous$$eyCode.A ) ) {
Fire();
}
}
function Fire() {
Debug.Log("Fire away!");
var clone : ParticleSystem;
//You need the "as ..." to assign it back to your `clone`
clone = Instantiate(prefab, transform.position, transform.rotation) as ParticleSystem;
}
I use Input.Get$$anonymous$$eyDown()
to make player press the correct key to fire the particle, this is for testing purpose only, just to check that your implementation on Fire()
is correct. Furthermore, I add in a Debug.Log()
in the Fire()
, when you Fire()
, you will able to see a log in the console (at the bottom left corner of the Unity Window)
If you want to mark my answer as the accepted answer, on the top left of my answer, there are 3 items: a thumb up, a number, and a thumb down; you, the OP, will have one extra item right below the thumb-down: a tick, click on that tick and you will be able to mark my answer as the correct answer.
Follow this Question
Related Questions
Applying Damage to enemy script not working 1 Answer
Error, I have no idea why :( 2 Answers
I need help converting this script from zilch to js 0 Answers