- Home /
Expecting : found ; error
Im wanting to make a script where whenever you get within Range #1 of a turret, the turret will look at you then start moving towards you. Then, when you get within Range #2, it will start shooting at you. I keep getting the expecting : found ; error and when I do as it says, it loads 3 new errors. Could someone please fix the script to where my intentions may be achieved? Here is the script:
var distanceTillShoot = 30;
var distanceTillLook = 15;
var target : Transform;
var damp = 6.0;
var laserPrefab : Transform;
var savedTime = 0;
var laserSpeed = 10000;
var targetTag : String;
var lookSwitch : Boolean = false;
var movementSpeed = 5;
function Start ()
{
var tar = GameObject.FindWithTag(targetTag);
if(tar)
lookAtTarget = tar.transform;
}
function Update ()
{
/* if(lookAtTarget)
{
var rotate = Quaternion.LookRotation(LookAtTarget.position - transform.position);
transform.rotation = Quaternion.Slerp(transform.rotation, rotate, Time.deltaTime * damp);
var seconds : int = Time.time;
var oddeven = (seconds % 50);
*/
if(lookAtTarget)
{
Look(seconds);
if(lookSwitch == true)
{ Shoot(seconds);
}
}
}
function Look(seconds)
{
var distance = Vector3.Distance(LookAtTarget.transform.position, transform.position);
if (distance <= distanceTillLook)
{
if(seconds!=savedTime)
lookSwitch = true;
}
{
transform.lookAt(target);
transform.Translate(Vector3.forward*movementSpeed*Time.deltaTime);
}
function Shoot(seconds)
{
var distance = Vector3.Distance(LookAtTarget.transform.position, transform.position);
if (distance <= distanceTillShoot)
{
if(seconds!=savedTime)
var laser = Instantiate(laserPrefab,transform.Find("spawnPointCanon").transform.position , transform.rotation);
}
{
gameObject.tag : "enemyProjectile";
laser.rigidbody.AddForce(transform.forward * laserSpeed);
}
{
savedTime=seconds;
}
if (distance > distanceTillShoot)
{
lookSwitch = false;
if(seconds!=savedTime)
}
}
Format your code first so we can read it without going cross-eyed.
The error is located at the "transform.lookAt(target);" section below function Shoot
Answer by Tasarran · Jan 26, 2012 at 07:50 AM
This code is fixed, but it doesn't look complete; there are two variables that haven't been declared (LookAtTarget and seconds), there is also an unfinished if statement toward the end...
But the compile errors are fixed.
There's a couple of things I think will help you, if you remember to do them: 1) whenever you start a new function or if statement, go ahead and type the closing brace, and type inside of the braces. This way, you'll never forget to add it. 2) always line up the closing braces with the line that they are closing, and indent anything inside. In other words, make sure the contents of the if statement are indented, and the closing brace is lined up even with the if line.
var distanceTillShoot = 30;
var distanceTillLook = 15;
var target : Transform;
var damp = 6.0;
var laserPrefab : Transform;
var savedTime = 0;
var laserSpeed = 10000;
var targetTag : String;
var lookSwitch : boolean = false;
var movementSpeed = 5;
function Start () {
var tar = GameObject.FindWithTag(targetTag);
if(tar) {
lookAtTarget = tar.transform;
}
}
function Update () {
if(LookAtTarget) {
Look(seconds);
if(lookSwitch == true) {
Shoot(seconds);
}
}
}
function Look(seconds) {
var distance = Vector3.Distance(LookAtTarget.transform.position, transform.position);
if (distance <= distanceTillLook) {
if(seconds!=savedTime)
lookSwitch = true;
}
transform.lookAt(target);
transform.Translate(Vector3.forward*movementSpeed*Time.deltaTime);
}
function Shoot(seconds) {
var distance = Vector3.Distance(LookAtTarget.transform.position, transform.position);
if (distance <= distanceTillShoot) {
if(seconds!=savedTime)
var laser = Instantiate(laserPrefab,transform.Find("spawnPointCanon").transform.position , transform.rotation);
}
gameObject.tag = "enemyProjectile";
laser.rigidbody.AddForce(transform.forward * laserSpeed);
savedTime=seconds;
if (distance > distanceTillShoot) {
lookSwitch = false;
if(seconds!=savedTime) {
}
}
}
Thanks for the help. It works for the range, but I'm still working on making it shoot when within the second range.
Answer by Tasarran · Jan 25, 2012 at 09:35 PM
The reason fixing one error is finding more is because there are multiple errors. Welcome to software debugging!
Pretty sure what's crashing you is the extra { just before the line
transform.lookAt(target);
in Look(seconds)
And the last part has messed up braces, too. You don't have an opening brace for the if statement
if(seconds!=savedTime)
but you do have a closing one. Also, why are you bracketing stand alone statements? If you just put:
{
statement;
statement;
}
That doesn't do anything except make your code harder to read. You do this two or three times in that last function...
Also, I think this line:
gameObject.tag : "enemyProjectile";
should be
gameObject.tag = "enemyProjectile";
Thanks for your input. Im still getting the same errors and since I'm new to scripting, I'm not knowing if I'm even placing the code in the right order or when to put brackets.
It helps if you do more than just copy-paste other people's code, though.
I don't just do that. I try to put my own script together. Though they are basic. Im self $$anonymous$$ching myself this stuff by looking at other peoples scripts and learning the keywords in each code.
Well, putting in matched sets of braces won't HURT you, i's just confusing.
The reason you put code in brackets is to say that it is to be executed together.
if you just put an if statement on on line, it automatically knows that the next thing is what it needs to execute. By putting braces around multiple statements, you tell the if statement that it needs to do ALL of those things.
Your answer
Follow this Question
Related Questions
How to activate / deactivate an enemy on player proximity 4 Answers
enemy waypoints and detection scipting 2 Answers
Enemy follows player then attacks! 2 Answers
How do I stop a function from executing? 1 Answer
Trap Door Question 1 Answer