I have a list that collects all TextMesh objects in the scene. I would like to sort it by hierarchy, but for some reason it seems to be sorting it by when it was created, ascending. I tried organizing them in the hierarchy but it doesn’t seem to make any difference. I am using
to populate my list. I tried using the .Orderby() Linq function, but I can’t really figure out how it works. Even sorting by position y would work, as my project is pretty well top to bottom.
ignore Linq whenever you can. It wasn’t made for games. don’t run away from it either, sometimes it’s useful.
you need to supply a lambda to your sorting function.
StarManta was quicker with a working example of a lambda.
I can’t get it to work, it says “Array does not exist in this context.” It could be because it’s a list? There seems to be a sort function if I type “texts.Sort” but it doesn’t work with your code there.
Also, how does this work exactly? what are “a” and “b”? Does this cycle through the whole list?
And yes, it goes through the list and sorts it. “a” and “b” are two hypothetical elements of the list that need to be compared. The sort function will use that comparison function over and over again so it knows what order things are supposed to go in. The actual algorithm used to sort is not important, just know that sorting will happen.
No errors now, but it is still sorting by most recent it appears… I tried reordering in hierarchy, and renaming, but it’s sorting by the most recently created ones. Which is pretty inconvenient because I have a script that changes the text based on the number in the List.
What are you basing this on? If you’re basing it on your print() statement in your code, note that that is logging the unmodified result. Do you have another debug log somewhere that has the order after you sort it?
Strange. I think my next step in your shoes would be to put in some logs to ensure that the code is being called when I expect it to be. So like:
texts.Sort<TextMeshProUGUI>(texts, (a, b) => {
Debug.Log($"Comparing {a.gameObject.name} at {a.transform.position} to {b.gameObject.name} at {b.transform.position}");
return a.transform.position.y.CompareTo(b.transform.position.y);
});
And you should see that log statement being outputted a bunch, covering a bunch of combinations of your text objects. This is just to make sure your sort code is being called when you expect it to.
Another possibility to look into would be the possibility that your “texts” field is getting overwritten at some point. In VS you can right click on texts and “Find All References”, and put a Debug.Log next to each line you see there that may be setting your array’s values.
One thing that’s a bit suspicious to me: there are text objects there that are NOT included in your array, which means they must have been created after FindObjectsOfType was run. If that’s the case, I wonder if there is more initialization that must be run before your sorting order is valid. Maybe try adding a one-frame delay to this searching and sorting thing to make sure that all the initialization is done running?
reposition it? You mean inside the array? Or inside the hierarchy? Have you checked the elements one by one as they appear in your array and compared the y coordinates of them?
I’m still not sure what exact criteria you want to use to sort your elements. Are you actually interested to sort them based on their phyiscal y position in the scene?
What are you trying to do that requires the List of TMP objects to be sorted by the order they appear in the hierarchy anyway? More often than not, sorting the collection isn’t the only solution.
Well it’s not entirely nonsensical. Maybe (s)he just wants to be able to author the order through hierarchy itself, which is a valid technique imo. Though your question is also valid, maybe there is a better solution, and the whole sorting ordeal is a conceptual mislead.
I’m trying to make a serializable list of editable text objects, that can be increased or decreased with the click of a button. The issue is, say I have 3 texts, it’s sorting them in order of ,“3,2,1.” so, if I make another one and add it to that list, it will be “3,2,1,4” then when I load the game again, it will load up the new one as 4,3,2,1, and the text will be all outta whack because i t will be loading the index “3” which will have been saved to number 4, into index 0 because number 4 is the top of the list now.
Edit: Each text object has an attached script which serializes it to PlayerPrefs based on the index of the manager object
your problem is weird. I’m not convinced it’s the right approach to solve serialization issues. you kind of messed something else up if your indices get shuffled every time you make a change.
The issue is for some reason it’s sorting by time of creation. I’m not sure why? I really didn’t do anything, the above script is the only one that populates the list.
But the issue is, if I create and add to the list during playtime, then after saving and quitting and loading again, it will repopulate the list.