x


Simple LookAt follow script

I am trying to make a simple following program. It looks like this

var target : GameObject;
var range : float;
var speed : float;
function Update () 
{
    range = Vector3.Distance(target.transform.position, transform.position);
    if (range<50)
    {
        transform.LookAt(target.transform.position);
    }
    if (range<40)
    {
        transform.Translate(Vector3.forward*speed*2);
    }
}

It kinda works, but I get this:

UnassignedReferenceException: The variable target of 'ai' has not been assigned. You probably need to assign the target variable of the ai script in the inspector. ai.Update () (at Assets/Enemy/ai.js:6)

Why?

more ▼

asked May 12 '11 at 06:40 AM

Max 4 gravatar image

Max 4
254 60 64 71

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

It means you didn't assign a gameobject to your varaible "target". You need to assign it in Inspector or assign it at run-time in script.

more ▼

answered May 12 '11 at 06:46 AM

Dreamer gravatar image

Dreamer
1.5k 39 49 66

But I have added it in the inspector.

May 12 '11 at 08:59 AM Max 4

do you add it in script in Assets or script in scene?

May 12 '11 at 12:30 PM Dreamer
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x329
x316

asked: May 12 '11 at 06:40 AM

Seen: 1565 times

Last Updated: May 12 '11 at 06:40 AM