【发布时间】:2017-07-24 20:44:52
【问题描述】:
我编写了一个脚本,将当前场景中的所有立方体组成一个数组:
public GameObject[] allCubes;
void Awake()
{
allCubes = GameObject.FindGameObjectsWithTag("cube");
}
问题是数组在检查器中看起来像这样:
https://i.gyazo.com/69f2f844183fe6e592e61c1517267da1.png
我已经尝试这样做了:
public GameObject[] allCubes;
void Awake()
{
allCubes = GameObject.FindGameObjectsWithTag("cube");
Array.Sort (allCubes);
}
但是,这给了我一个错误:
InvalidOperationException: No IComparable or IComparable<UnityEngine.GameObject> interface found.
System.Array.compare[GameObject] (UnityEngine.GameObject value1, UnityEngine.GameObject value2, IComparer`1 comparer) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System/Array.cs:1756)
System.Array.qsort[GameObject,GameObject] (UnityEngine.GameObject[] keys, UnityEngine.GameObject[] items, Int32 low0, Int32 high0, IComparer`1 comparer) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System/Array.cs:1722)
System.Array.Sort[GameObject,GameObject] (UnityEngine.GameObject[] keys, UnityEngine.GameObject[] items, Int32 index, Int32 length, IComparer`1 comparer) (at
我该怎么办?
【问题讨论】:
-
那么您希望它如何对
GameObjects 进行排序? -
或者,一个相关的问题 - 对它们进行排序的目的是什么?
-
@DavidG 我希望它们的排序类似于 Cube1 - Cube2 - Cube2....但我觉得我错了..
-
@Adrian 对于我的代码,我使用“+1”在数组中“向下滚动”。
-
您需要进行字母数字排序。检查我刚刚留下的新答案。