- Home /
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'.
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!
Answer by Ejlersen · Dec 28, 2010 at 06:54 PM
Try:
transform.LookAt(home.transform, Vector3.up);
or it can be transform.LookAt(home.transform.position); and for specifically axis transform.LookAt(home.transform.position.y);
it workes for like the first 4 seconds then i get NullReferenceException
Can you post the error that the debug log is giving? The LookAt(home.transform) should work fine forever.
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, ins$$anonymous$$d of calling find on every update (this is expensive).
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
Answer by Ejlersen · Dec 28, 2010 at 07:32 PM
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.
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)'.