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 Sendatsu_Yoshimitsu · Nov 24, 2014 at 07:07 AM · c#linq

Confusion over LINQ syntax

I'm trying to use LINQ's OrderBy to organize a list by distance, such that the following function will always organize myList[0] to be the closest gameobject to myObject:

 void SortObjectsByDistance(List<GameObject> myList, GameObject myObject){
     myList = (List<GameObject>)myList.OrderBy (myObject => (myObject.transform.position - transform.position).sqrMagnitude);
 }

However, this produces an error which makes me think my syntax is off: "a local variable named 'myObject' cannot be declared in this scope because it would give a different meaning to 'myObject', which is already used in a parent or current scope to denote something else". There's nothing called myObject outside of this function, which makes me think I'm using LINQ wrong- do I need to make a third, junk variable and assign it to myObject somehow?

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
0
Best Answer

Answer by fafase · Nov 24, 2014 at 07:24 AM

   myList = myList
         .OrderBy(myObject=>Vector3.Distance(myObject.transform.position,transform.position)
         .ToList();



EDIT: I removed one set of () around the parameter of Distance as there were two sets. Her below is a demo program that is tested and working:

 List<GameObject> myList = new List<GameObject>();
 void Start () 
 {
     GameObject objA = new GameObject();
     objA.transform.position = new Vector3(5,0,0);
     GameObject objB = new GameObject();
     objB.transform.position = new Vector3(2,0,0);
     GameObject objC = new GameObject();
     objC.transform.position = new Vector3(7,0,0);
     GameObject objD = new GameObject();
     objD.transform.position = new Vector3(6,0,0);
     GameObject objE = new GameObject();
     objE.transform.position = new Vector3(1,0,0);

     myList = new List<GameObject>(){objA, objB, objC, objD, objE };

     myList = myList.OrderBy(myObject => Vector3.Distance(myObject.transform.position,transform.position)).ToList();
 }


Comment
Add comment · Show 3 · 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 Sendatsu_Yoshimitsu · Nov 24, 2014 at 07:30 AM 0
Share

Hmm, I might be doing something wrong, but replacing the contents of my function with this produces three errors- the original "give a different meaning to myObject" one, an unexpected ',' on the .OrderBy line, and an internal compiler error during parsing.

avatar image fafase · Nov 24, 2014 at 08:04 AM 0
Share

You had a myObject as parameter, which was the different meaning error. The myObject within the OrderBy is created by the OrderBy method. This is a little tricky to understant but what you are doing is that you tell the compiler to fetch an item from the list and name it myObject periodically, then use it in the Distance method against another variable that in this case is the object holding the script.

Just like when you use foreach:

 foreach (GameObject myObject in myList){
     // iterate through myList, each item is called myObject 
     // inside the foreach loop
     float distance = Vector3.Distance(myObject.transform.position,
                     transform.position);
     // $$anonymous$$ore sorting here
 } 



avatar image Sendatsu_Yoshimitsu · Nov 24, 2014 at 08:47 AM 0
Share

That works!! Thank you very much- my problem was misunderstanding how OrderBy worked, I thought myObject was something you passed to it that it compared against the item in the list, not the holding value as you moved the list around. :)

avatar image
0

Answer by tanoshimi · Nov 24, 2014 at 07:45 AM

"There's nothing called myObject outside of this function". No, but there is already a (unnecessary) local parameter called myObject specified in the function signature, which is accessible from within the lambda's scope and therefore conflicts with the declaration of myObject there.

I also think you'll have problems with your approach of casting the result back to your List. Try this instead:

 void SortObjectsByDistance(List<GameObject> myList){
     myList = myList.OrderBy(myObject => (myObject.transform.position - transform.position).sqrMagnitude).ToList();
 }
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 Sendatsu_Yoshimitsu · Nov 24, 2014 at 07:52 AM 0
Share

This compiles, but I'm a little confused- if I'm not feeding it myObject when the function runs, how and where would I specify which object myObject holds a reference to?

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

26 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

[Solved] How to LINQ to ArrayList all the Transform within a Range? 1 Answer

Trouble with LINQ syntax: "Gameobject must be convertible to Component in order to use it as parameter T" 1 Answer

Distribute terrain in zones 3 Answers

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Multiple Cars not working 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