• 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 programming 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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



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

creating and manipulating a grid efficiently 1 Answer

Creating a List before Runtime 0 Answers

How can I connect a set of variables to a unique identifier? 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges