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
0
Question by code-blep · Sep 30, 2012 at 02:29 PM · arraydistancenamesort

Find Nearest Object from an Array of Transforms

Hi,

I have an array of Transforms (Each with it's own unique name), and I am trying to get the name of the Transform that is nearest to another Object.

The variable I have used to setup the array is:

 var attackerClonesArray = new List.<Transform>();

I have tried adapting various solutions (for example Eric5r5's comments in this post: http://answers.unity3d.com/questions/15229/geting-the-closest-object-from-a-array.html) but I have had little luck.

I understand that I will have to create another array to sort through, but this seems to cause my problems. It's almost like I am creating the wrong type or structure of array if that makes sense.

Any advice will be most welcome!

Thanks

Paul

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

1 Reply

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

Answer by whydoidoit · Sep 30, 2012 at 03:20 PM

You can do it with Linq if you like:

   import System.Linq;
 ...
 
 var nearest = yourArrayOfTransforms.OrderBy(function(t) { return (thingToBeCloseTo.position - t.position).sqrMagnitude; }).First();
Comment
Add comment · Show 10 · 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 whydoidoit · Sep 30, 2012 at 03:21 PM 0
Share

For best performance cache thingToBeCloseTo.position before the line:

   var checkPosition = thingToBeCloseTo.position;

   var nearest = yourArrayOfTransforms.OrderBy(function(t) { return (checkPosition - t.position).sqr$$anonymous$$agnitude; }).First();
avatar image code-blep · Sep 30, 2012 at 09:27 PM 0
Share

Hi $$anonymous$$ike,

Thanks for the suggestion. I am having problems implementing it. Here is what I have so far:

[code]

import System.Linq;

var thingToBeCloseTo = Vector3();

function Update() {

thingToBeCloseTo = transform.position;

var nearest = attackerClonesArray.OrderBy(function(t) { return (thingToBeCloseTo - t.position).sqr$$anonymous$$agnitude; }).First(); }

[/code]

I am getting the following error code: $$anonymous$$issing$$anonymous$$ethodException: $$anonymous$$ethod not found: 'System.Collections.Generic.List`1[[UnityEngine.Transform, UnityEngine, Version=0.0.0.0, Culture=neutral, Public$$anonymous$$eyToken=null]].OrderBy'.

It late now so I am maybe being a bit stupid >_<

Also how do you specify if a section is code here in the comment section?

Thanks!

avatar image whydoidoit · Sep 30, 2012 at 09:29 PM 0
Share

Do you have stripping turned on? Are you using a very low version of the .NET mscorlib?

avatar image code-blep · Sep 30, 2012 at 09:35 PM 0
Share

Er... I have no idea about what you just referenced lol!

I just looked up StrippingLevel and I have not setup anything to do that. So I would say whatever the default is.

As for a very low level .NET mscorlib, sorry but I have no idea, so I would guess that as a no also.

Sorry!

avatar image whydoidoit · Sep 30, 2012 at 10:03 PM 0
Share

It's build > player settings - Api compatibility.

Show more comments

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

11 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

Related Questions

Resize Array based on distance. 2 Answers

Storing a variable and it's name 3 Answers

Can an Instance Array Recognize Separately Named Instances? 0 Answers

Sort - OrderBy GameObjects by Distance 2 Answers

List.Sort with IComparer 2 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