I’m trying to sort an array of waypoint object each starting with ‘WP’ followed by a number from 1 - 9. The waypoints have a parent object which holds the script and each contain a spawnpoint object as a child. Each of the waypoints also have a ‘Waypoint’ script attached which is how I’m adding them to an array to begin with.
The code also gives the following error:
ArgumentException: does not implement right interface
System.Collections.Generic.Comparer`1+DefaultComparer[UnityEngine.GameObject].Compare (UnityEngine.GameObject x, UnityEngine.GameObject y)
System.Linq.SortSequenceContext`2[Waypoint,UnityEngine.GameObject].Compare (Int32 first_index, Int32 second_index)
System.Linq.QuickSort`1[Waypoint].CompareItems (Int32 first_index, Int32 second_index)
System.Linq.QuickSort`1[Waypoint].MedianOfThree (Int32 left, Int32 right)
System.Linq.QuickSort`1[Waypoint].Sort (Int32 left, Int32 right)
System.Linq.QuickSort`1[Waypoint].PerformSort ()
System.Linq.QuickSort`1+<Sort>c__Iterator21[Waypoint].MoveNext ()
System.Collections.Generic.List`1[Waypoint].AddEnumerable (IEnumerable`1 enumerable)
System.Collections.Generic.List`1[Waypoint]..ctor (IEnumerable`1 collection)
System.Linq.Enumerable.ToArray[Waypoint] (IEnumerable`1 source)
WPGroup.Start () (at Assets/016 - Move Object Randomly with Radius/WPGroup.cs:17)
The code I am working with is as follows;
using System.Linq;
public Waypoint[] WPGroupList = new Waypoint[9];
public GameObject[] WPgoGroupList = new GameObject[9];
// Use this for initialization
void Start ()
{
// Searches entire scene - just want to search this group
//WPGroupList = GameObject.FindGameObjectsWithTag("Waypoint");
// Pass all waypoints inside object into array
WPGroupList = gameObject.GetComponentsInChildren<Waypoint>(); // Select(go => go.transform).ToArray();
// Order array in ascending order - not working
WPGroupList.OrderBy(go => go.name).ToArray();
// Created array of GameObjects afterwards to test if problem was that it was
// having trouble because they were waypoints
for (int i = 0; i < WPGroupList.Length; i++)
{
// Copy complete gameObjects from WP list into this GameObject array
WPgoGroupList _= WPGroupList*.gameObject;*_
}
// Try to sort that - not working
//WPgoGroupList.OrderBy(go=>go.name).ToArray();
}