Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Jenkins6161 · Jul 31, 2013 at 02:38 AM · objectdirectionnearest

Get nearest object and find direction problem

Trying to find the nearest object and then get the direction from that object to the player. Keep getting the error "'transform' is not a member of 'Object'." which I don't understand because the object it refers to is being set to a GameObject which should have transform available to it!

 function FixedUpdate() {
 
      if (EQ&&thrown) {
         //Objects to get direction
         var player = GameObject.Find("FPSController");
         var target = GetNearestTaggedObject();
         
         //Vector from target to player
         var heading = (player.transform.position - target.transform.position);
         var distance = heading.magnitude;
         var direction = heading/distance;
         
         //Push player away from target
         player.transform.Translate(direction * .5 * Time.deltaTime, Space.World);
     }
 }
 
 function GetNearestTaggedObject() {
  
     var nearestDistanceSqr = Mathf.Infinity;
     var taggedGameObjects = GameObject.FindGameObjectsWithTag(searchTag); 
     var nearestObj = null;
  
     for (var obj : GameObject in taggedGameObjects) {
  
         var objectPos = obj.transform.position;
         var distanceSqr = (objectPos - transform.position).sqrMagnitude;
  
         if (distanceSqr < nearestDistanceSqr) {
             nearestObj = obj.transform;
             nearestDistanceSqr = distanceSqr;
         }
     }
  
     return nearestObj;
 }
Comment
Add comment · Show 3
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 highpockets · Jul 31, 2013 at 03:15 AM 0
Share

On line 27, I'm assu$$anonymous$$g you mean to put 'player.transform.postition' ins$$anonymous$$d of just transform.position, right?

avatar image Jenkins6161 · Jul 31, 2013 at 03:25 AM 0
Share

umm tbh I copied the GetNearestTagedObject from another questions answer. I don't think it should need to be player.transform.position since its setting that to one of the tagged objects. But then again I'm a novice and have no idea what I'm doing...

avatar image highpockets · Jul 31, 2013 at 03:37 AM 0
Share

If you just say transform.position, it will calculate the position of the object the script is attached to. Since you had to find the player object in update, the script is not attached to the player object. If it is, this ma go without saying, but you don't need to find the player object just call it gameObject.transform...

2 Replies

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

Answer by aldonaletto · Jul 31, 2013 at 03:31 AM

You should declare the variable types. If not mentioned, JS tries to infer the type from the value you're using to initialize the variable - if there's no initialization value, the variable is assumed to be Object, which can hold any type. In this particular case, the problem probably is caused by this line:

    var target = GetNearestTaggedObject();

GetNearestTaggedObject has no return type, neither the type of target is explicitly declared, thus it's assumed to be Object. Change this line to:

    var target: Transform = GetNearestTaggedObject();

You should also declare the return type of GetNearestTaggedObject and the type of nearestObj:

    function GetNearestTaggedObject(): Transform { // declare the return type
        var nearestDistanceSqr = Mathf.Infinity;
        var taggedGameObjects = GameObject.FindGameObjectsWithTag(searchTag); 
        var nearestObj: Transform = null; // null doesn't define the type
        ...

As a rule of thumb, always declare the type of any variable that doesn't have an initialization value. Declare also the type of variables initialized with untyped values (like nearestObj - null has no specific type).

Comment
Add comment · 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 Jenkins6161 · Jul 31, 2013 at 05:25 AM 0
Share

Wow that worked. I had that to begin with but removed it because it was giving me errors. Guess I needed to set my variable like the function not the function to the variable. Thanks!

avatar image
0

Answer by highpockets · Jul 31, 2013 at 03:29 AM

Actually I missed that the player object is declared in the update function, but not in the other. Put line 5 where you declare your player object outside of the update function so both functions can use it and put what I mentioned in my last comment on line 27. Hope it works for you.

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 Jenkins6161 · Jul 31, 2013 at 03:33 AM 0
Share

error 'transform' is not a member of 'Object'. on line 9 still

specifically on the target variable. The player variable doesn't have errors

avatar image highpockets · Jul 31, 2013 at 04:06 AM 0
Share

Oops, listen to Aldon, I'm used to C#

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

17 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Object following the mouse click point? 1 Answer

How to have an object moving to one direction and rotate it whitous affecting the direction 1 Answer

How to change forward direction of ball to where camera is facing? 0 Answers

Swipe direction on an object 0 Answers

Unable to see custom object in Inspector. 0 Answers


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