Im having trouble with my turrets AI

Im having trouble with my turrets AI script in javascript. When I try to play the game it comes up with variouse errors which are in the link below, one of the main errors is an “unexpected token”…please help.


Here the code:

var LookAtTarget:Transform;
var damp = 2.0;
var bulletPrefab:Transform;
var savedTime=0;

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 % 2);
        
        if(oddeven)
        {
            Shoot(seconds);
        }
    }
}

function Shoot(seconds);
{
    if(seconds!=savedTime);
    {
        var bullet = Instantiate(bulletPrefab ,transform.Find("Sphere").transform.position ,Quaternion.identity);
        
        bullet.rigidbody.AddForce(transform.forward * 1500);
        
        savedTime=seconds;
     }
}

You shouldnt have a semicolon after Shoot(seconds) and after (seconds!=savedTime)

The last two should fix after you remove the semicolons.

I’m not sure if it is only C# but you should have a cast on your instantiate
var bullet = Instantiate(bulletPrefab ,transform.Find(“Sphere”).transform.position ,Quaternion.identity) as GameObject;

Your new Shoot function

function Shoot(seconds)
{
    if(seconds!=savedTime)
    {
        var bullet = Instantiate(bulletPrefab ,transform.Find("Sphere").transform.position ,Quaternion.identity) as GameObject;

        bullet.rigidbody.AddForce(transform.forward * 1500);

        savedTime=seconds;
     }
}

I shall try it out now. Thankyou!

I am now having another error pop up but I am able to play the game but the turret doesn’t follow the player and shoot at him.

Error:

NullReferenceException
TurretAI.Shoot (System.Object seconds) (at Assets/Game/BasicGameMechanics/TurretAI/TurretAI.js:32)
TurretAI.Update () (at Assets/Game/BasicGameMechanics/TurretAI/TurretAI.js:19)