Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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
1
Question by henry96 · Sep 22, 2011 at 06:24 AM · javascriptgameobjecttransformtargetfind

Find Transform in the scene

Hello, everybody! Now I'm having a little problem with my scripting. This is my code of my AI.

 var target : Transform;

 function Awake ()
 
 {
 
      if (!target)
      {
 
          target = GameObject.FindWithTag ("Player");
      }
 }

//the code above has error. "Cant Convert GameObject to Transform". In my function update, I need to use the transform in order to make my AI rotate and move toward the Player. i.e :

  var relativePos = target.position - transform.position;

So how can I fix that? Thanks in advance!

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
7
Best Answer

Answer by CHPedersen · Sep 22, 2011 at 06:29 AM

You're almost there. :)

Your problem is, as the error points out, that GameObject.FindWithTag returns a GameObject, and you want a transform. Fortunately, GameObjects always have a transform. (Since, understandably enough, it's hard to define an object in your game with less detail than at least a position, scale and rotation).

Therefore, to acquire the player's transform, all you have to do is add ".transform" to that:

 target = GameObject.FindWithTag ("Player").transform;

Then you're accessing the transform of the GameObject tagged "Player", and not just trying to save the reference to the GameObject itself.

Comment
Add comment · Show 2 · 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 henry96 · Sep 22, 2011 at 06:47 AM 0
Share

wow!!! Thank you!!! It works!!! ^^

avatar image ownerfate · Aug 13, 2013 at 09:55 AM 0
Share

CHPedersen. Thank you so much for you're help and Henry for asking the question

this just saved a ton of trail and error. Thank you.

avatar image
1

Answer by alok.kr.029.hotmail · Mar 11, 2014 at 08:49 AM

target = GameObject.Find("Hero").transform;

Comment
Add comment · 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

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.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Finding object with transform 1 Answer

"GameObject.Find();" not working 2 Answers

Else Statement Not Working 1 Answer

Locating Position of Object not Working? 2 Answers

FindClosestEnemy() change target? 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