x


java script error

This is the script can nayone fix the error

static var comehome: boolean = false;

static var runaround: boolean = true;

var speed:float = 20; var rotatespeed:float = 10; var dis:float = 1; var LeftRight:float = 2; var infront:float = 1; var home : String; private var direction:float = 60;

function Start() { var home =gameObject.FindWithTag(home); } function Update(){ if(comehome){ //transform.LookAt(home); transform.LookAt(home.transform, Vector3.up); transform.Translate(Vector3.forward*speed*Time.deltaTime); }

if(runaround){

if(!Physics.Raycast(transform.position, transform.forward, infront)){
        transform.Translate(Vector3.forward * speed * Time.deltaTime);
    }
    else{
        if(!Physics.Raycast(transform.position, -transform.right, LeftRight)){
            direction = 60;
        }
        else if(!Physics.Raycast(transform.position, transform.right, LeftRight)){
            direction = -60;
        }
        transform.Rotate(Vector3.up, 60 * rotatespeed * Time.deltaTime * direction);
    }

} }

this is the error

BCE0019: 'transform' is not a member of 'String'.

more ▼

asked Dec 28 '10 at 06:50 PM

zeuo gravatar image

zeuo
373 52 70 83

Please make sure all of your code is formatted with the code markup (select all the lines while editing your question, then click the 01 icon), to make it easier to read. Thanks!

Dec 28 '10 at 10:58 PM yoyo
(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

Try:

transform.LookAt(home.transform, Vector3.up);
more ▼

answered Dec 28 '10 at 06:54 PM

Ejlersen gravatar image

Ejlersen
1.3k 1 6 11

or it can be transform.LookAt(home.transform.position); and for specifically axis transform.LookAt(home.transform.position.y);

Dec 28 '10 at 06:56 PM Uriel_96

it workes for like the first 4 seconds then i get NullReferenceException

Dec 28 '10 at 06:57 PM zeuo

Can you post the error that the debug log is giving? The LookAt(home.transform) should work fine forever.

Dec 28 '10 at 07:23 PM Bob5602

Well, if it suddenly can't find any object with the tag home. Then it will give null refs. So if you check for this, then you should never get a null ref. But here is a question for you. If "home" is always the same game object, then cache it, instead of calling find on every update (this is expensive).

Dec 28 '10 at 07:27 PM Ejlersen

the problem is that i cant even get it to work, anything i try im always getting errors. i just simply want the lookat to look at home and home is a tag thats all

Dec 28 '10 at 07:52 PM zeuo
(comments are locked)
10|3000 characters needed characters left

As I mentioned in my last comment. You could save some performance if "home" is the same game object at all times. Like this:


var home : GameObject;
var comeHome : boolean = true;

function Start()
{
    home = gameObject.FindWithTag("home");
}

function Update()
{
    if (comeHome && home != null)
    {
         transform.LookAt(home.transform);
         transform.Translate(Vector3.forward * speed * Time.deltaTime);
    }
}

You see the difference? Now "home" is a string, which is the tag name. Whereas the variable home is of the type GameObject.

As you probably know. String is just a bunch of letters or chars ('a', 'b', 'c'), whereas GameObject is a class. With an instance of GameObject (also known as an object), you can refer to the transform of the game object. Transform is required of the: transform.LookAt(Transform) method.

more ▼

answered Dec 28 '10 at 07:32 PM

Ejlersen gravatar image

Ejlersen
1.3k 1 6 11

when i try that it says

The best overload for the method 'UnityEngine.GameObject.FindWithTag(String)' is not compatible with the argument list '(UnityEngine.GameObject)'.

Dec 28 '10 at 08:01 PM zeuo
(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:

x327
x198
x111

asked: Dec 28 '10 at 06:50 PM

Seen: 1122 times

Last Updated: Dec 28 '10 at 07:56 PM