Compiler error??

This is the code I am using to make a simple character have AI, why isn’t it working? It says that ‘Assets/AIgremlin.js(15,1): BCE0044: expecting }, found ‘’.’ My code is this

#pragma strict

function Start ()
{
    var theplayer : GameObject;
    var speed : float;
    var range : int;
    var explosion : GameObject;
}

function Update ()
{
    range = Vector3.Distance(theplayer.transform.rotation,transform.position);
    if (range < 40)
    {
        transform.LookAt(theplayer.transform.position);
    }

Well

  1. You appear to be defining variables inside Start when they should be outside of it to allow other functions to access them.
  2. You are finding the distance between a rotation and a position which doesn’t make sense!

I guess you want your script to look like this:

#pragma strict

var theplayer : GameObject;
var range : float = 40.0;

function Update ()
{
    var distance = Vector3.Distance(theplayer.transform.position, transform.position);
    if (distance < range)
    {
        transform.LookAt(theplayer.transform.position);
    }
}

I’ve removed everything that isn’t used anywhere by this script. Of course you have to assign your player gameobject to the “theplayer” variable in the inspector