• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
Question by zeuo · Dec 28, 2010 at 06:50 PM · lookatsyntax-errorbce0019

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'.

Comment

People who like this

0 Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image yoyo · Dec 28, 2010 at 10:58 PM 0
Share

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!

2 Replies

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by Ejlersen · Dec 28, 2010 at 06:54 PM

Try:

transform.LookAt(home.transform, Vector3.up);
Comment
Bob5602
zeuo

People who like this

2 Show 9 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Uriel_96 · Dec 28, 2010 at 06:56 PM 0
Share

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

avatar image zeuo · Dec 28, 2010 at 06:57 PM 0
Share

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

avatar image Bob5602 · Dec 28, 2010 at 07:23 PM 0
Share

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

avatar image Ejlersen · Dec 28, 2010 at 07:27 PM 0
Share

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).

avatar image zeuo · Dec 28, 2010 at 07:52 PM 0
Share

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

Show more comments
avatar image
Best Answer

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.

Comment
zeuo

People who like this

1 Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image zeuo · Dec 28, 2010 at 08:01 PM 0
Share

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)'.

Your answer

Hint: You can notify a user about this post by typing @username

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

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

"is not a member" 1 Answer

Accessing array in another script - Error message 2 Answers

Script Error (js) 1 Answer

Getting an error while script reference says it's ok 1 Answer

I got a major question 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges