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 kidshenlong · Mar 31, 2013 at 03:33 AM · c#listslinq

Understanding Linq and orderbydescending

Hey everyone, was wondering if I could get some help.

I'm ordering a list of objects using Linq, which I'm pretty unfamiliar with but it's been going so okay so far. Well it was until I moved some script around.

 myList = myList.OrderByDescending(x => x.GetComponent<CarCheckpoint>().currentprogress).ToList();

I was using the above line of code. I didn't entirely understand how it worked but I assumed that x was the value of the object in the list and by using a simple GetComponent I could grab the value I wanted the order the list by. It was working fine until I moved this script into a new file and attached it to a different object. Before it was part of the object's main script.

 myList = myList.OrderByDescending(x => x.GetComponent<playerposition>().currentprogress).ToList();

So made some slight revisions to the code and this was result. But now it just throws u NullReference exception, so I assume it isn't successfully finding this value.

I've looked around the internet but would still like some light shed on a few things.

Does x represent the items in the list or the object it is attached to?

I am currently trying to grab an int from a script attached to an object inside the list, if the above code is incorrect how instead should I do this?

Sorry to be a pain guys, just this script is giving me a headache. Would really appreciate any clarity on this as the documentation is giving me a headache.

Thanks.

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

Answer by Matthew A · Mar 31, 2013 at 10:44 AM

x in the lambda expression is an item in the list (a GameObject from the looks of your code).

If you're getting a null reference exception, either your list is null, a GameObject in your list is null (may have been destroyed), or a GameObject doesn't have the correct component attached. You may have to refine your list to weed out possible problems:

 GameObject[] allObjects = GameObject.FindGameObjectsWithTag("Foo"); // or however you're getting the original list...
 
 var goodObjects = allObjects.Where(o => o != null && o.GetComponent() != null);
 
 var result = goodObjects.OrderByDescending(o => o.GetComponent().progress);
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 kidshenlong · Mar 31, 2013 at 02:16 PM 0
Share

Thanks so much for you help. So much more I need to learn! The issue was as you said. I hadn't attached the racer script to every object just 1. Lol late night program$$anonymous$$g ay? Thanks for your help.

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

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

List/Array should handle another List/Array - Upgrade System - Performance 1 Answer

[Answered](C#)Comparing two Lists for a Crafting System 2 Answers

How do I compare two lists for equality (not caring about order) C# 6 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