- Home /
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);
}
This code is receiving the error "Assets/AIgremlin.js(15,1): BCE0044: expecting }, found ''." can someone please explain why this is.
#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); }
I've converted your answer to a comment, on Unity Answers - Answer means solution and not reply. There's an add new comment button hidden on the right of the screen.
I have also formatted your code- to do this either indent your code by 4 spaces or 1 tab before pasting or highlight it all and click the code button after pasting.
$$anonymous$$y codes working now but the enemy doesn't look at me, is there a problem with my code?
#pragma strict
function Update ()
{
var theplayer : GameObject;
var speed : float;
var range : int;
var explosion : GameObject;
range=Vector3.Distance(theplayer.transform.position,transform.position);
if (range<40)
{
transform.LookAt(theplayer.transform.position);
}
}
Answer by whydoidoit · Aug 11, 2012 at 09:50 AM
Well
You appear to be defining variables inside Start when they should be outside of it to allow other functions to access them.
You are finding the distance between a rotation and a position which doesn't make sense!
Well put the variables outside Start and use position not rotation
@Glinny @seth-bergman
I removed your two comments since they are not longer relevant and the whole question is already kinda messy.
Answer by Bunny83 · Aug 12, 2012 at 01:14 AM
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
@Glinny: To your first quiestion: I have no idea. This script doesn't move anything, it just rotates the object so the z axis looks towards the given object. $$anonymous$$aybe you attached the player controller script to your enemy as well?
Your second question has nothing to do with my answer nor with the question you've asked here, so post it as a new question if you can't find the solution on UnityAnswers. This might help