おかげで、これはマニュアルで指定されておらず、予測不可能なように返しFindGameObjectsWithTag
項目の順序に依存することはありません。配列をループし、GameObject.name
プロパティと比較することによって、指定したGameObjectを見つけるカスタム関数を追加する必要があります。
GameObject[] players;
void test()
{
players = GameObject.FindGameObjectsWithTag("Player");
foreach (GameObject go in players)
{
Debug.Log("Player " + go + " is named " + go.name);
}
}
GameObject getGameObject(string gameObjectName)
{
for (int i = 0; i < players.Length; i++)
{
//Return GameObject if the name Matches
if (players[i].name == gameObjectName)
{
return players[i];
}
}
Debug.Log("No GameObject with the name \"" + gameObjectName + "\" found in the array");
//No Match found, return null
return null;
}
使用:
GameObject card1 = getGameObject("card1");
GameObject card2 = getGameObject("card2");
GameObject card3 = getGameObject("card3");
GameObject card4 = getGameObject("card4");
EDIT:
あなたの目標は、これはそれを行う必要があり、順番に、配列内のアイテムをソートする場合:
players = GameObject.FindGameObjectsWithTag("Player");
players = players.OrderBy(c => c.name).ToArray();
あなたはそれを自分でソートする必要があります –